如何以编程方式获取iOS设备MAC地址

时间:2013-02-12 07:15:59

标签: ios mac-address

如何在我的应用程序中以编程方式获取iOS设备的MAC代码?

5 个答案:

答案 0 :(得分:17)

现在iOS 7设备 - 始终返回的地址为02:00:00:00:00:00。

更好地使用[UIDevice identifierForVendor]

最好将此方法称为应用专用的唯一键

类别会更合适

#import "UIDevice+Identifier.h"

- (NSString *) identifierForVendor1
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
        return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    }
    return @"";
}

现在调用上述方法获取唯一地址

NSString *like_UDID=[NSString stringWithFormat:@"%@",
                [[UIDevice currentDevice] identifierForVendor1]];

NSLog(@"%@",like_UDID);

答案 1 :(得分:10)

您可以使用以下功能获取MAC地址:

+(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;              
    mgmtInfoBase[3] = AF_LINK;        // Request link layer information
    mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces

    // With all configured interfaces requested, get handle index
    if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0) 
        errorFlag = @"if_nametoindex failure";
    // Get the size of the data available (store in len)
    else if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0) 
        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
    else if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
    {
        free(msgBuffer);
        errorFlag = @"sysctl msgBuffer failure";
    }
    else
    {
        // 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);

        // Copy link layer address data in socket structure to an array
        unsigned char macAddress[6];
        memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);

        // Read from char array into a string object, into traditional Mac address format
        NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
                                      macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]];
        if(IsDEBUG) NSLog(@"Mac Address: %@", macAddressString);

        // Release the buffer memory
        free(msgBuffer);

        return macAddressString;
    }

    // Error...
    if(IsDEBUG) NSLog(@"Error: %@", errorFlag);

    return errorFlag;
}

但正如他所说#Randomclik,ios 7及以上版本无法使用mac地址。

形成苹果:

在iOS 7及更高版本中,如果您要求提供iOS设备的MAC地址,系统将返回值02:00:00:00:00:00。如果需要识别设备,请改用UIDevice的identifierForVendor属性。 (需要为自己的广告目的使用标识符的应用应考虑使用ASIdentifierManager的advertisingIdentifier属性。)

链接: https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW1

关于MAC地址的对话:

How can I programmatically get the MAC address of an iphone

答案 2 :(得分:2)

似乎在iOS 7及更高版本中获取MAC地址将无法正常工作。看看Apple的iOS文档有什么新内容。 (不推荐使用的API部分。)

答案 3 :(得分:1)

每个人都建议使用官方的ios 7方式并使用[UIDevice identifierForVendor]

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

并考虑从一些较旧的假设中迁移。

答案 4 :(得分:0)

我猜你可以通过结合以下两个答案来获得它。

首先,使用以下命令查找设备的IP:

https://stackoverflow.com/a/30754194/1089206

其次,使用以下命令查找该地址的MAC:

https://stackoverflow.com/a/31246085/1089206

我现在会尝试一下,并会告诉你它是怎么回事。