iPhone哪些情况下无法成功获取MAC地址?

5
使用以下方法,在99.5%的情况下能够正常工作,但是在一些AppStore用户反馈无法获取mac地址(我们使用标识符对每个服务器调用进行身份验证)。
失败的情况基于IOS版本为5.0.1的iPhone 4S。
除了低内存之外,您知道还有哪些情况会导致失败吗?
/* 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;              
    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]];
        NSLog(@"Mac Address: %@", macAddressString);
        
        // Release the buffer memory
        free(msgBuffer);
        
        return macAddressString;
    }
    
    // Error...
    NSLog(@"Error: %@", errorFlag);
    
    return errorFlag;
}

非常感谢您的帮助。


有关此问题的任何经验吗? - jianhua
你能重现这个错误吗?我刚在 iPhone 4s 5.1.1 上测试了你的代码(这是我手头唯一的设备),它完美地运行了。我可以推荐我个人使用的方法,但它与你所拥有的方法非常相似... - Miles Alden
1
en0不就是Wi-Fi接口吗? 如果设备上的该接口失效了会发生什么? - jaseelder
你假设设备实际上有一个en0接口(例如,并非所有iOS设备都具有WiFi)。 - clarkcox3
1
@clarkcox3:所有的iOS设备都有Wi-Fi功能。该功能可能被用户禁用,此时en0接口可能不存在(不确定其工作原理),但是所有的iOS设备都有(过)Wi-Fi功能。 - Scott Berrevoets
@scott 不,你是错误的。有许多iOS设备根本没有WiFi硬件(例如最初在中国销售的iPhone 3G就没有WiFi)。 - clarkcox3
1个回答

0
我在将 iPhone 升级到 iOS 5 后遇到了这样的情况,问题是臭名昭著的 wifi bug。你可能不会喜欢这个答案,但唯一的解决方案是将设备送回 Apple 并获得一个正常工作的新设备。代码可以更改以在 MAC 地址查找失败时不崩溃,但当 REST 调用需要唯一标识符时,这并没有帮助。作为开发人员,你陷入了进退两难的境地。

是的,你说得对,不需要在这种问题上浪费时间。 - jianhua

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接