iOS开发

获取苹果手机的信息(手机型号、IP地址)

2018-04-11  本文已影响12人  风规自远

//

//  iPhoneInfoTool.h

//  M

//

//  Created by zsl on 2017/11/1.

//  Copyright © 2017年 zsl. All rights reserved.

//

#import

@interfaceiPhoneInfoTool :NSObject

/**

 根据操作系统获取苹果设备名称

 @return 苹果设备名称名称

 */

+  (NSString *)getThePhoneNameAccordingToTheOperatingSystem;

/**

 * 获取设备的IP地址

 */

+  (NSString *)getDeviceIpAddress;

@end






//

//  iPhoneInfoTool.m

//  M

//

//  Created by zsl on 2017/11/1.

//  Copyright © 2017年 zsl. All rights reserved.

//

#import "iPhoneInfoTool.h"

/**1、获取设备名称导入头文件*/

#import "sys/utsname.h"

/**2、获取IP地址导入头文件*/

#include

#include // Per msqr

#import

#include

#import

@implementation iPhoneInfoTool

/**

 * 设备名称

 */

+ (NSString*)getThePhoneNameAccordingToTheOperatingSystem {


    //需要#import "sys/utsname.h"

    structutsnamesystemInfo;

    uname(&systemInfo);

    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    //iPhone1,1为型号标识符

    if([deviceStringisEqualToString:@"iPhone1,1"])    return@"iPhone 1G";

    if([deviceStringisEqualToString:@"iPhone1,2"])    return@"iPhone 3G";

    if([deviceStringisEqualToString:@"iPhone2,1"])    return@"iPhone 3GS";

    if([deviceStringisEqualToString:@"iPhone3,1"])    return@"iPhone 4";

    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";

    if([deviceStringisEqualToString:@"iPhone4,1"])    return@"iPhone 4S";

    if([deviceStringisEqualToString:@"iPhone5,1"])    return@"iPhone 5";

    if([deviceStringisEqualToString:@"iPhone5,2"])    return@"iPhone 5";

    if([deviceStringisEqualToString:@"iPhone5,3"])    return@"iPhone 5C";

    if([deviceStringisEqualToString:@"iPhone5,4"])    return@"iPhone 5C";

    if([deviceStringisEqualToString:@"iPhone6,1"])    return@"iPhone 5S";

    if([deviceStringisEqualToString:@"iPhone6,2"])    return@"iPhone 5S";

    if([deviceStringisEqualToString:@"iPhone7,1"])    return@"iPhone 6 Plus";

    if([deviceStringisEqualToString:@"iPhone7,2"])    return@"iPhone 6";

    if([deviceStringisEqualToString:@"iPhone8,1"])    return@"iPhone 6s";

    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";

    if([deviceStringisEqualToString:@"iPhone8,4"])    return@"iPhone SE";

    if([deviceStringisEqualToString:@"iPhone9,1"])    return@"iPhone 7";

    if([deviceStringisEqualToString:@"iPhone9,3"])    return@"iPhone 7";

    if([deviceStringisEqualToString:@"iPhone9,2"])    return@"iPhone 7 Plus";

    if([deviceStringisEqualToString:@"iPhone9,4"])    return@"iPhone 7 Plus";

    if([deviceStringisEqualToString:@"iPhone10,1"])  return@"iPhone 8";

    if([deviceStringisEqualToString:@"iPhone10,4"])  return@"iPhone 8";

    if ([deviceString isEqualToString:@"iPhone10,2"])  return @"iPhone 8 Plus";

    if ([deviceString isEqualToString:@"iPhone10,5"])  return @"iPhone 8 Plus";

    if([deviceStringisEqualToString:@"iPhone10,3"])  return@"iPhone X";

    if([deviceStringisEqualToString:@"iPhone10,6"])  return@"iPhone X";

    // iPod touch

    if([deviceStringisEqualToString:@"iPod1,1"])      return@"iPod Touch 1G";

    if([deviceStringisEqualToString:@"iPod2,1"])      return@"iPod Touch 2G";

    if([deviceStringisEqualToString:@"iPod3,1"])      return@"iPod Touch 3G";

    if([deviceStringisEqualToString:@"iPod4,1"])      return@"iPod Touch 4G";

    if ([deviceString isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";

    // iPad 平板电脑

    if([deviceStringisEqualToString:@"iPad1,1"])      return@"iPad";

    if([deviceStringisEqualToString:@"iPad1,2"])      return@"iPad 3G";

    if([deviceStringisEqualToString:@"iPad2,1"])      return@"iPad 2 (WiFi)";

    if([deviceStringisEqualToString:@"iPad2,2"])      return@"iPad 2";

    if([deviceStringisEqualToString:@"iPad2,3"])      return@"iPad 2 (CDMA)";

    if([deviceStringisEqualToString:@"iPad2,4"])      return@"iPad 2";

    if([deviceStringisEqualToString:@"iPad2,5"])      return@"iPad Mini (WiFi)";

    if([deviceStringisEqualToString:@"iPad2,6"])      return@"iPad Mini";

    if ([deviceString isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";

    if([deviceStringisEqualToString:@"iPad3,1"])      return@"iPad 3 (WiFi)";

    if([deviceStringisEqualToString:@"iPad3,2"])      return@"iPad 3 (GSM+CDMA)";

    if([deviceStringisEqualToString:@"iPad3,3"])      return@"iPad 3";

    if([deviceStringisEqualToString:@"iPad3,4"])      return@"iPad 4 (WiFi)";

    if([deviceStringisEqualToString:@"iPad3,5"])      return@"iPad 4";

    if([deviceStringisEqualToString:@"iPad3,6"])      return@"iPad 4 (GSM+CDMA)";

    if([deviceStringisEqualToString:@"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([deviceStringisEqualToString:@"iPad4,6"])      return@"iPad Mini 2";

    if([deviceStringisEqualToString:@"iPad4,7"])      return@"iPad Mini 3";

    if([deviceStringisEqualToString:@"iPad4,8"])      return@"iPad Mini 3";

    if([deviceStringisEqualToString:@"iPad4,9"])      return@"iPad Mini 3";

    if ([deviceString isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (WiFi)";

    if([deviceStringisEqualToString:@"iPad5,2"])      return@"iPad Mini 4 (LTE)";

    if([deviceStringisEqualToString:@"iPad5,3"])      return@"iPad Air 2";

    if([deviceStringisEqualToString:@"iPad5,4"])      return@"iPad Air 2";

    if([deviceStringisEqualToString:@"iPad6,3"])      return@"iPad Pro 9.7";

    if([deviceStringisEqualToString:@"iPad6,4"])      return@"iPad Pro 9.7";

    if([deviceStringisEqualToString:@"iPad6,7"])      return@"iPad Pro 12.9";

    if([deviceStringisEqualToString:@"iPad6,8"])      return@"iPad Pro 12.9";

    if([deviceStringisEqualToString:@"AppleTV2,1"])    return@"Apple TV 2";

    if([deviceStringisEqualToString:@"AppleTV3,1"])    return@"Apple TV 3";

    if([deviceStringisEqualToString:@"AppleTV3,2"])    return@"Apple TV 3";

    if([deviceStringisEqualToString:@"AppleTV5,3"])    return@"Apple TV 4";

    if([deviceStringisEqualToString:@"i386"])        return@"Simulator";

    if([deviceStringisEqualToString:@"x86_64"])      return@"Simulator";


    returndeviceString;


}

/**

 * 设备IP地址

 */

+ (NSString*)getDeviceIpAddress {


    intsockfd =socket(AF_INET,SOCK_DGRAM,0);


    NSMutableArray *ips = [NSMutableArray array];


    intBUFFERSIZE =4096;


    structifconfifc;


    charbuffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;


    structifreq*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 = (structifreq*)ptr;

            intlen =sizeof(structsockaddr);


            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)];

            [ipsaddObject:ip];

        }

    }

    close(sockfd);

    NSString*deviceIP =@"";


    for(inti=0; i < ips.count; i++) {

        if(ips.count>0) {

            deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject];

        }

    }

    returndeviceIP;

}

@end

上一篇下一篇

猜你喜欢

热点阅读