In order to collect user information and deliver advertisements, the server often needs all kinds of information from mobile phone users’ devices and APPS. Here is how to obtain all kinds of information:









Click here to download the GitHub source code for the above display

  • A line of code to fix the unification!
    // This method will be listed later
    NSString *deviceName = [self getDeviceName];
    NSLog(@" Device model -->%@", deviceName);

    NSString *iPhoneName = [UIDevice currentDevice].name;
    NSLog(@ "iPhone name - > % @", iPhoneName); 

    NSString *appVerion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSLog(@" APP version number -->%@", appVerion);

    CGFloat batteryLevel = [[UIDevice currentDevice] batteryLevel];
    NSLog(@" Battery level -->%f", batteryLevel);

    NSString *localizedModel = [UIDevice currentDevice].localizedModel;
    NSLog(@"localizedModel-->%@", localizedModel);

    NSString *systemName = [UIDevice currentDevice].systemName;
    NSLog(@" Current system name -->%@", systemName);

    NSString *systemVersion = [UIDevice currentDevice].systemVersion;
    NSLog(@" Current system version number -->%@", systemVersion);

    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *device_model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    NSLog(@"device_model-->%@", device_model);

    // This method is listed separately later
    NSString *macAddress = [self getMacAddress];
    NSLog(@"macAddress-->%@", macAddress);

    // This method is listed separately later
    NSString *deviceIP = [self getDeviceIPAddresses];
    NSLog(@"deviceIP-->%@", deviceIP);Copy the code
  • Advertising space identifier: All App will take on the same equipment to the same value, is Apple specifically to the advertising provider used to track the user, the user can reset in privacy Settings | | advertising tracking this id value, or to limit the use of this id, so id may take less than the value, but fortunately, the default is to allow the tracking of Apple, And the average user does not know there is such a setting, so basically used to monitor the promotion effect, is too much poke

      NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
      NSLog(@"AD space identifier IDFA -->%@", idfa);Copy the code
  • UUID is an abbreviation of Universally Unique Identifier. It allows all elements in a distributed system to have unique identification information, without the need for the identification information through the central control terminal. This way, each person can create a UUID that doesn’t conflict with anyone else. In this case, there is no need to worry about name duplication when the database is created. Apple recommends using UUID to generate unique identification strings for applications.

     NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
     NSLog(@" unique identifier uUID -->%@", uuid);Copy the code
  • Obtaining the device model

// Obtain the device model and manually convert it to the corresponding name
- (NSString *)getDeviceName
{
    #import "sys/utsname.h"# Warning summary! The most comprehensive! Close test! The whole network only this one!! struct utsname systemInfo; uname(&systemInfo); NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];if ([deviceString isEqualToString:@"However, 1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"However, 2"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"However, 3"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"The iphone 4, 1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"The iphone 5, 1"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"The iphone 5, 2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"The iphone 5, 3"])    return @"iPhone 5c (GSM)";
    if ([deviceString isEqualToString:@"The iphone 5, 4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([deviceString isEqualToString:@"IPhone6, 1"])    return @"iPhone 5s (GSM)";
    if ([deviceString isEqualToString:@"IPhone6, 2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([deviceString isEqualToString:@"IPhone7, 1"])    return @"iPhone 6 Plus";
    if ([deviceString isEqualToString:@"IPhone7, 2"])    return @"iPhone 6";
    if ([deviceString isEqualToString:@"IPhone8, 1"])    return @"iPhone 6s";
    if ([deviceString isEqualToString:@"IPhone8, 2"])    return @"iPhone 6s Plus";
    if ([deviceString isEqualToString:@"IPhone8, 4"])    return @"iPhone SE";
    // The two Japanese phone models are exclusive to Japan and may use SONY FeliCa payment scheme instead of Apple Pay
    if ([deviceString isEqualToString:@"IPhone9, 1"])    return @"China Bank, Japan Version, Hong Kong Bank iPhone 7";
    if ([deviceString isEqualToString:@"IPhone9, 2"])    return @IPhone 7 Plus for Hong Kong bank and China Bank;
    if ([deviceString isEqualToString:@"IPhone9, 3"])    return @"IPhone 7 for US and Taiwan";
    if ([deviceString isEqualToString:@"IPhone9, 4"])    return @"IPhone 7 Plus for US and Taiwan";

    if ([deviceString isEqualToString:@"IPod1, 1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"IPod2, 1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"IPod3, 1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"IPod4, 1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"IPod5, 1"])      return @"iPod Touch (5 Gen)";

    if ([deviceString isEqualToString:@"IPad1, 1"])      return @"iPad";
    if ([deviceString isEqualToString:@"IPad1, 2"])      return @"iPad 3G";
    if ([deviceString isEqualToString:@"And 1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"And 2"])      return @"iPad 2";
    if ([deviceString isEqualToString:@"And 3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"And 4"])      return @"iPad 2";
    if ([deviceString isEqualToString:@"And 5"])      return @"iPad Mini (WiFi)";
    if ([deviceString isEqualToString:@"And 6"])      return @"iPad Mini";
    if ([deviceString isEqualToString:@"And 7"])      return @"iPad Mini (GSM+CDMA)";
    if ([deviceString isEqualToString:@"IPad3, 1"])      return @"iPad 3 (WiFi)";
    if ([deviceString isEqualToString:@"IPad3, 2"])      return @"iPad 3 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"IPad3, 3"])      return @"iPad 3";
    if ([deviceString isEqualToString:@"IPad3, 4"])      return @"iPad 4 (WiFi)";
    if ([deviceString isEqualToString:@"Ipad 3, 5"])      return @"iPad 4";
    if ([deviceString isEqualToString:@"IPad3, 6"])      return @"iPad 4 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"IPad4, 1"])      return @"iPad Air (WiFi)";
    if ([deviceString isEqualToString:@"IPad4, 2"])      return @"iPad Air (Cellular)";
    if ([deviceString isEqualToString:@"IPad4, 4"])      return @"iPad Mini 2 (WiFi)";
    if ([deviceString isEqualToString:@"IPad4, 5"])      return @"iPad Mini 2 (Cellular)";
    if ([deviceString isEqualToString:@"IPad4, 6"])      return @"iPad Mini 2";
    if ([deviceString isEqualToString:@"IPad4, 7"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"IPad4, 8"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"IPad4, 9"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"Lofty, 1"])      return @"iPad Mini 4 (WiFi)";
    if ([deviceString isEqualToString:@"The lofty, 2"])      return @"iPad Mini 4 (LTE)";
    if ([deviceString isEqualToString:@"The lofty, 3"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"Lofty, 4"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"IPad6, 3"])      return @"The Pro 9.7";
    if ([deviceString isEqualToString:@"IPad6, 4"])      return @"The Pro 9.7";
    if ([deviceString isEqualToString:@"IPad6, 7"])      return @"The Pro 12.9";
    if ([deviceString isEqualToString:@"IPad6, 8"])      return @"The Pro 12.9";

    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";

    return deviceString;
}Copy the code
  • The MAC address
- (NSString *)getMacAddress {
    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");
        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 uppercaseString];
}Copy the code
  • The IP address
- (NSString *)getDeviceIPAddresses {

    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    NSMutableArray *ips = [NSMutableArray array];

    int BUFFERSIZE = 4096;

    struct ifconf ifc;

    char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;

    struct ifreq *ifr, ifrcopy;

    ifc.ifc_len = BUFFERSIZE;
    ifc.ifc_buf = buffer;

    if (ioctl(sockfd, SIOCGIFCONF, &ifc) >= 0) {for(ptr = buffer; ptr < buffer + ifc.ifc_len; ) { ifr = (struct ifreq *)ptr;
            int len = sizeof(struct sockaddr);

            if (ifr->ifr_addr.sa_len > len) {
                len = ifr->ifr_addr.sa_len;
            }

            ptr += sizeof(ifr->ifr_name) + len;
            if(ifr->ifr_addr.sa_family ! = AF_INET)continue;
            if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) *cptr = 0;
            if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) continue;

            memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
            ifrcopy = *ifr;
            ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);

            if ((ifrcopy.ifr_flags & IFF_UP) == 0) continue;

            NSString *ip = [NSString  stringWithFormat:@"%s", inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];
            [ips addObject:ip];
        }
    }

    close(sockfd);
    NSString *deviceIP = @"";

    for (int i=0; i < ips.count; i++) {
        if (ips.count > 0) {
            deviceIP = [NSString stringWithFormat:@"% @",ips.lastObject]; }}return deviceIP;
}Copy the code