Take the 2-minute tour ×

Use the following method and work well for 99.5% cases, but very few users in AppStore report they can't get mac address (we use the identifier to authenticate every server call).

The failed case is based on iPhone 4S with IOS version 5.0.1.

Hey guys,

Do you know for which case (besides low memory) it will fail? Any idea or discussion will be appreciated. Thanks.

/* Original source code courtesy John from iOSDeveloperTips.com */

#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

- (NSString *)getMacAddress
    int                 mgmtInfoBase[6];
    char                *msgBuffer = NULL;
    NSString            *errorFlag = NULL;
    size_t              length;

    // Setup the management Information Base (mib)
    mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
    mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
    mgmtInfoBase[2] = 0;              

    // With all configured interfaces requested, get handle index
        errorFlag = @"if_nametoindex failure";
        errorFlag = @"sysctl mgmtInfoBase failure";
    // Alloc memory based on above call
    else if ((msgBuffer = malloc(length)) == NULL)
        errorFlag = @"buffer allocation failure";
    // Get system information, store in buffer
        errorFlag = @"sysctl msgBuffer failure";
        // Map msgbuffer to interface message structure
        struct if_msghdr *interfaceMsgStruct = (struct if_msghdr *) msgBuffer;

        // Map to link-level socket structure
        struct sockaddr_dl *socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);

        unsigned char macAddress[6];

                                      macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]];

        // Release the buffer memory

        return macAddressString;

    // Error...

    return errorFlag;

Any help is greatly appreciated.

share|improve this question
Any experience on this issue? –  jianhua Nov 12 '12 at 1:40
Have you been able to recreate this error? I just tested your code on iPhone 4s 5.1.1 (all I had to hand) and it works just dandy. I could recommend the methods I use personally, but it's pretty similar to what you have... –  Miles Alden Jan 15 '13 at 20:54
en0 is only the wifi interface isn't it? What happens if that interface is down on the device? –  jaseelder Jan 15 '13 at 22:46
You’re assuming that the device actually has an en0 interface (not all iOS devices have WiFi, for instance). –  clarkcox3 May 21 '13 at 20:25
@clarkcox3: All iOS devices have Wi-Fi. It may be disabled by the user, at which point the en0 interface may be non-existent (not sure how that works), but all iOS devices have (had) Wi-Fi. –  Scott Berrevoets May 27 '13 at 21:08
show 1 more comment

1 Answer

up vote 0 down vote accepted

I ran into a situation like this after upgrading an iPhone to iOS 5 the problem was the infamous wifi bug. You are not going to like this answer, but the only solution was to take the device back to Apple and get a new one that worked properly. The code can be changed to not crash when the mac address lookup fails, but that does not help when REST calls require a unique identifier. As a developer, you are stuck between a rock and a hard place.

share|improve this answer
Yes, you are right, no need to cost time on this kind of issue. –  jianhua Aug 14 '13 at 23:56
add comment

Your Answer


