How can I programmatically get the MAC address of an iphone

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

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

相关标签:
12条回答
  • 2020-11-22 08:35

    Update: this will not work on iOS 7. You should use ASIdentifierManager.


    More clean solution on MobileDeveloperTips website:

    #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;
      size_t              length;
      unsigned char       macAddress[6];
      struct if_msghdr    *interfaceMsgStruct;
      struct sockaddr_dl  *socketStruct;
      NSString            *errorFlag = NULL;
    
      // 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";
      else
      {
        // Get the size of the data available (store in len)
        if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0) 
          errorFlag = @"sysctl mgmtInfoBase failure";
        else
        {
          // Alloc memory based on above call
          if ((msgBuffer = malloc(length)) == NULL)
            errorFlag = @"buffer allocation failure";
          else
          {
            // Get system information, store in buffer
            if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
              errorFlag = @"sysctl msgBuffer failure";
          }
        }
      }
    
      // Befor going any further...
      if (errorFlag != NULL)
      {
        NSLog(@"Error: %@", errorFlag);
        return errorFlag;
      }
    
      // Map msgbuffer to interface message structure
      interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
    
      // Map to link-level socket structure
      socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
    
      // Copy link layer address data in socket structure to an array
      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;
    }
    
    0 讨论(0)
  • 2020-11-22 08:37

    There are vary solutions about this, but I couldn't find a whole thing. So I made my own solution for :

    nicinfo

    How to use :

    NICInfoSummary* summary = [[[NICInfoSummary alloc] init] autorelease];
    
    // en0 is for WiFi 
    NICInfo* wifi_info = [summary findNICInfo:@"en0"];
    
    // you can get mac address in 'XX-XX-XX-XX-XX-XX' form
    NSString* mac_address = [wifi_info getMacAddressWithSeparator:@"-"];
    
    // ip can be multiple
    if(wifi_info.nicIPInfos.count > 0)
    {
        NICIPInfo* ip_info = [wifi_info.nicIPInfos objectAtIndex:0];
        NSString* ip = ip_info.ip;
        NSString* netmask = ip_info.netmask;
        NSString* broadcast_ip = ip_info.broadcastIP;
    }
    else
    {
        NSLog(@"WiFi not connected!");
    }
    
    0 讨论(0)
  • 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 @"?.?.?.?";
    }
    
    0 讨论(0)
  • 2020-11-22 08:41

    @Grantland This "pretty clean solution" looks similar to my own improvement over iPhoneDeveloperTips solution.

    You can see my step here: https://gist.github.com/1409855/

    /* 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 nil;
    }
    
    0 讨论(0)
  • 2020-11-22 08:41
    #import <sys/socket.h>
    #import <net/if_dl.h>
    #import <ifaddrs.h>
    #import <sys/xattr.h>
    #define IFT_ETHER 0x6
    

    ...

    - (NSString*)macAddress
    {
        NSString* result = nil;
    
        char* macAddressString = (char*)malloc(18);
        if (macAddressString != NULL)
        {
            strcpy(macAddressString, "");
    
            struct ifaddrs* addrs = NULL;
            struct ifaddrs* cursor;
    
            if (getifaddrs(&addrs) == 0)
            {
                cursor = addrs;
    
                while (cursor != NULL)
                {
                    if ((cursor->ifa_addr->sa_family == AF_LINK) && (((const struct sockaddr_dl*)cursor->ifa_addr)->sdl_type == IFT_ETHER) && strcmp("en0", cursor->ifa_name) == 0)
                    {
                        const struct sockaddr_dl* dlAddr = (const struct sockaddr_dl*) cursor->ifa_addr;
                        const unsigned char* base = (const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen];
    
                        for (NSInteger index = 0; index < dlAddr->sdl_alen; index++)
                        {
                            char partialAddr[3];
    
                            sprintf(partialAddr, "%02X", base[index]);
                            strcat(macAddressString, partialAddr);
                        }
                    }
    
                    cursor = cursor->ifa_next;
                }
    
            }
    
            result = [[[NSString alloc] initWithUTF8String:macAddressString] autorelease];
    
            free(macAddressString);
        }
    
        return result;
    }
    
    0 讨论(0)
  • 2020-11-22 08:42

    It's not possible anymore on devices running iOS 7.0 or later, thus unavailable to get MAC address in Swift.

    As Apple stated:

    In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02:00:00:00:00:00. If you need to identify the device, use the identifierForVendor property of UIDevice instead. (Apps that need an identifier for their own advertising purposes should consider using the advertisingIdentifier property of ASIdentifierManager instead.)

    0 讨论(0)
提交回复
热议问题