How can I programmatically get the MAC address of an iphone

后端 未结 12 2170
春和景丽
春和景丽 2020-11-22 08:06

How to programmatically get an iPhone\'s MAC address and IP address?

12条回答
  •  故里飘歌
    2020-11-22 08:39

    A lot of these questions only address the Mac address. If you also require the IP address I just wrote this, may need some work but seems to work well on my machine...

    - (NSString *)getLocalIPAddress
    {
        NSArray *ipAddresses = [[NSHost currentHost] addresses];
        NSArray *sortedIPAddresses = [ipAddresses sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        numberFormatter.allowsFloats = NO;
    
        for (NSString *potentialIPAddress in sortedIPAddresses)
        {
            if ([potentialIPAddress isEqualToString:@"127.0.0.1"]) {
                continue;
            }
    
            NSArray *ipParts = [potentialIPAddress componentsSeparatedByString:@"."];
    
            BOOL isMatch = YES;
    
            for (NSString *ipPart in ipParts) {
                if (![numberFormatter numberFromString:ipPart]) {
                    isMatch = NO;
                    break;
                }
            }
            if (isMatch) {
                return potentialIPAddress;
            }
        }
    
        // No IP found
        return @"?.?.?.?";
    }
    

提交回复
热议问题