How can I programmatically get the MAC address of an iphone

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

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

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

    To create a uniqueString based on unique identifier of device in iOS 6:

    #import <AdSupport/ASIdentifierManager.h>
    
    NSString *uniqueString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSLog(@"uniqueString: %@", uniqueString);
    
    0 讨论(0)
  • 2020-11-22 08:26

    Starting from iOS 7, the system always returns the value 02:00:00:00:00:00 when you ask for the MAC address on any device.

    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.)"

    Reference: releasenotes

    0 讨论(0)
  • 2020-11-22 08:27

    I wanted something to return the address regardless of whether or not wifi was enabled, so the chosen solution didn't work for me. I used another call I found on some forum after some tweaking. I ended up with the following (excuse my rusty C ) :

    #include <sys/types.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <net/if_dl.h>
    #include <ifaddrs.h>
    
    
    char*  getMacAddress(char* macAddress, char* ifName) {
    
    int  success;
    struct ifaddrs * addrs;
    struct ifaddrs * cursor;
    const struct sockaddr_dl * dlAddr;
    const unsigned char* base;
    int i;
    
    success = getifaddrs(&addrs) == 0;
    if (success) {
        cursor = addrs;
        while (cursor != 0) {
            if ( (cursor->ifa_addr->sa_family == AF_LINK)
                && (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type == IFT_ETHER) && strcmp(ifName,  cursor->ifa_name)==0 ) {
                dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
                base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen];
                strcpy(macAddress, ""); 
                for (i = 0; i < dlAddr->sdl_alen; i++) {
                    if (i != 0) {
                        strcat(macAddress, ":");
                    }
                    char partialAddr[3];
                    sprintf(partialAddr, "%02X", base[i]);
                    strcat(macAddress, partialAddr);
    
                }
            }
            cursor = cursor->ifa_next;
        }
    
        freeifaddrs(addrs);
    }    
    return macAddress;
    }
    

    And then I would call it asking for en0, as follows:

    char* macAddressString= (char*)malloc(18);
    NSString* macAddress= [[NSString alloc] initWithCString:getMacAddress(macAddressString, "en0")
                                                  encoding:NSMacOSRomanStringEncoding];
    free(macAddressString);
    
    0 讨论(0)
  • 2020-11-22 08:30

    Now iOS 7 devices – are always returning a MAC address of 02:00:00:00:00:00.

    So better use [UIDevice identifierForVendor].

    so better to call this method to get app specific unique key

    Category will more suitable

    import "UIDevice+Identifier.h"

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

    Now call above method to get unique address

    NSString *like_UDID=[NSString stringWithFormat:@"%@",
                    [[UIDevice currentDevice] identifierForVendor1]];
    
    NSLog(@"%@",like_UDID);
    
    0 讨论(0)
  • 2020-11-22 08:33

    NOTE As of iOS7, you can no longer retrieve device MAC addresses. A fixed value will be returned rather than the actual MAC


    Somthing I stumbled across a while ago. Originally from here I modified it a bit and cleaned things up.

    IPAddress.h
    IPAddress.c

    And to use it

    InitAddresses();
    GetIPAddresses();
    GetHWAddresses();
    
    int i;
    NSString *deviceIP = nil;
    for (i=0; i<MAXADDRS; ++i)
    {
        static unsigned long localHost = 0x7F000001;        // 127.0.0.1
        unsigned long theAddr;
    
        theAddr = ip_addrs[i];
    
        if (theAddr == 0) break;
        if (theAddr == localHost) continue;
    
        NSLog(@"Name: %s MAC: %s IP: %s\n", if_names[i], hw_addrs[i], ip_names[i]);
    
            //decided what adapter you want details for
        if (strncmp(if_names[i], "en", 2) == 0)
        {
            NSLog(@"Adapter en has a IP of %s", ip_names[i]);
        }
    }
    

    Adapter names vary depending on the simulator/device as well as wifi or cell on the device.

    0 讨论(0)
  • 2020-11-22 08:34

    This looks like a pretty clean solution: UIDevice BIdentifier

    // Return the local MAC addy
    // Courtesy of FreeBSD hackers email list
    // Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
    - (NSString *) macaddress{
    
        int                 mib[6];
        size_t              len;
        char                *buf;
        unsigned char       *ptr;
        struct if_msghdr    *ifm;
        struct sockaddr_dl  *sdl;
    
        mib[0] = CTL_NET;
        mib[1] = AF_ROUTE;
        mib[2] = 0;
        mib[3] = AF_LINK;
        mib[4] = NET_RT_IFLIST;
    
        if ((mib[5] = if_nametoindex("en0")) == 0) {
            printf("Error: if_nametoindex error\n");
            return NULL;
        }
    
        if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
            printf("Error: sysctl, take 1\n");
            return NULL;
        }
    
        if ((buf = malloc(len)) == NULL) {
            printf("Could not allocate memory. error!\n");
            return NULL;
        }
    
        if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
            printf("Error: sysctl, take 2");
            free(buf);
            return NULL;
        }
    
        ifm = (struct if_msghdr *)buf;
        sdl = (struct sockaddr_dl *)(ifm + 1);
        ptr = (unsigned char *)LLADDR(sdl);
        NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", 
                               *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
        free(buf);
    
        return outstring;
    }
    
    0 讨论(0)
提交回复
热议问题