【问题标题】:How to get iOS device MAC address programmatically如何以编程方式获取 iOS 设备 MAC 地址
【发布时间】:2013-01-27 10:52:49
【问题描述】:

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

【问题讨论】:

标签: ios mac-address


【解决方案1】:

现在 iOS 7 设备 - 总是返回 02:00:00:00:00:00 的 MAC 地址。

所以最好使用[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);

【讨论】:

    【解决方案2】:

    您可以使用以下函数获取 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,mac 地址在 ios 7 及更高版本中不可用。

    形成苹果:

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

    链接: 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

    【讨论】:

      【解决方案3】:

      似乎在 iOS 7 及更高版本中获取 MAC 地址将不起作用。查看来自 Apple 的 iOS 文档的新增功能。 (已弃用的 API 部分。)

      【讨论】:

        【解决方案4】:

        建议大家使用官方的 ios 7 方式并使用 [UIDevice identifierForVendor]

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

        还要考虑一些旧假设的迁移。

        【讨论】:

          【解决方案5】:

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

          首先,通过以下方式找到设备的ip:

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

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

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

          我现在就试一试,然后告诉你进展如何。

          【讨论】:

          • 请避免使用链接。是的,即使在 SO 中。请将链接更改为其中所说内容的简短摘要。将链接添加为附加项是没有问题的,但这不是一个容易理解答案的方法。
          猜你喜欢
          • 1970-01-01
          • 2011-05-07
          • 2012-06-05
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          • 2012-07-27
          • 2020-05-06
          • 1970-01-01
          相关资源
          最近更新 更多