在进行iOS开发过程中,我们有时候需要获取设备的一些信息。下面整理了一些常用的获取设备信息的方法。
1.获取系统版本号
[[UIDevice currentDevice] systemVersion];
2. 获取设备名称
[[UIDevice currentDevice] name];
3.获取APP版本
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
4.获取运营商名称
- 需要先导入头文件
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
- 获取运营商名称
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [info subscriberCellularProvider];
NSString *carrierName = [NSString stringWithFormat:@"%@",[carrier carrierName]];
5.获取IP地址
IP地址分为内网和公网的IP地址
- 获取内网IP
// 需要导入的包
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <net/if.h>
// 宏定义
#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
#define IOS_VPN @"utun0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"
- (NSString *)getIPAddress {
NSArray *searchArray = @[ IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6,
IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/"
IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ];
NSDictionary *addresses = [self getAddresses];
__block NSString *address;
[searchArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL
* _Nonnull stop) {
address = addresses[obj];
NSLog(@"key = %@ ; object = %@",obj,addresses[obj]);
if([self isValidateIP:address]) *stop = YES;
}];
return address ? address : @"0.0.0.0";
}
- (BOOL)isValidateIP:(NSString *)ipAddress {
if (ipAddress.length == 0) {
return NO;
}
NSString *urlRegEx = @"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
NSError *error;
NSRegularExpression *regex =
[NSRegularExpressionregularExpressionWithPattern:urlRegEx options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:ipAddress
options:0 range:NSMakeRange(0, [ipAddress length])];
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
NSString *result = [ipAddress substringWithRange:resultRange];
NSLog(@"result = %@",result);
return YES;
}
}
return NO;
}
- (NSDictionary *)getAddresses {
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
struct ifaddrs *interfaces;
if (!getifaddrs(&interfaces)) {
struct ifaddrs *interface;
for (interface=interfaces; interface; interface=interface->ifa_next) {
if (!(interface->ifa_flags & IFF_UP)) {
continue;
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface-
>ifa_addr;
char addrBuf[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
if (addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface-
>ifa_name];
NSString *type;
if (addr->sin_family == AF_INET) {
if (inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 *addr6 = (const struct
sockaddr_in6*)interface->ifa_addr;
if (inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf,
INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if (type) {
NSString *key = [NSString stringWithFormat:@"%@/%@",name,type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}
- 获取公网IP
NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?
ip=myip"];
NSData *data = [NSData dataWithContentsOfURL:ipURL];
NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString *ipStr = nil;
if (ipDic && [ipDic[@"code"] integerValue]== 0) {
ipStr = ipDic[@"data"][@"ip"];
}
return (ipStr ? ipStr : @"");
6.获取设备的型号
需要获取的型号格式如:iPhone 6s Plus
参考: https://www.theiphonewiki.com/wiki/Models,这个是苹果官方的设备类型说明。
//需要导入的包
#import <sys/utsname.h>
//获取设备型号
- (NSString *)getDeviceModelName {
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceModel = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
NSString *name = [self getVersionName:deviceModel];
return name;
}
- (NSString *)getVersionName:(NSString *)deviceModel {
//iPhone系列
if ([deviceModel isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([deviceModel isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
if ([deviceModel isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
if ([deviceModel isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([deviceModel isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([deviceModel isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([deviceModel isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
if ([deviceModel isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
if ([deviceModel isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
if ([deviceModel isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
if ([deviceModel isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([deviceModel isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([deviceModel isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([deviceModel isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
if ([deviceModel isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
if ([deviceModel isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
if ([deviceModel isEqualToString:@"iPhone9,3"]) return @"iPhone 7";
if ([deviceModel isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
if ([deviceModel isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus";
//iPod系列
if ([deviceModel isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([deviceModel isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([deviceModel isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([deviceModel isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([deviceModel isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
if ([deviceModel isEqualToString:@"iPod7,1"]) return @"iPod Touch 7G";
//iPad系列
if ([deviceModel isEqualToString:@"iPad1,1"]) return @"iPad";
if ([deviceModel isEqualToString:@"iPad2,1"]) return @"iPad 2";
if ([deviceModel isEqualToString:@"iPad2,2"]) return @"iPad 2";
if ([deviceModel isEqualToString:@"iPad2,3"]) return @"iPad 2";
if ([deviceModel isEqualToString:@"iPad2,4"]) return @"iPad 2";
if ([deviceModel isEqualToString:@"iPad2,5"]) return @"iPad mini";
if ([deviceModel isEqualToString:@"iPad2,6"]) return @"iPad mini";
if ([deviceModel isEqualToString:@"iPad2,7"]) return @"iPad mini";
if ([deviceModel isEqualToString:@"iPad4,4"]) return @"iPad mini 2";
if ([deviceModel isEqualToString:@"iPad4,5"]) return @"iPad mini 2";
if ([deviceModel isEqualToString:@"iPad4,6"]) return @"iPad mini 2";
if ([deviceModel isEqualToString:@"iPad4,7"]) return @"iPad mini 3";
if ([deviceModel isEqualToString:@"iPad4,8"]) return @"iPad mini 3";
if ([deviceModel isEqualToString:@"iPad4,9"]) return @"iPad mini 3";
if ([deviceModel isEqualToString:@"iPad5,1"]) return @"iPad mini 4";
if ([deviceModel isEqualToString:@"iPad5,2"]) return @"iPad mini 4";
if ([deviceModel isEqualToString:@"iPad3,1"]) return @"iPad 3G";
if ([deviceModel isEqualToString:@"iPad3,2"]) return @"iPad 3G";
if ([deviceModel isEqualToString:@"iPad3,3"]) return @"iPad 3G";
if ([deviceModel isEqualToString:@"iPad3,4"]) return @"iPad 4G";
if ([deviceModel isEqualToString:@"iPad3,5"]) return @"iPad 4G";
if ([deviceModel isEqualToString:@"iPad3,6"]) return @"iPad 4G";
if ([deviceModel isEqualToString:@"iPad6,11"]) return @"iPad 5G";
if ([deviceModel isEqualToString:@"iPad6,12"]) return @"iPad 5G";
if ([deviceModel isEqualToString:@"iPad4,1"]) return @"iPad Air";
if ([deviceModel isEqualToString:@"iPad4,2"]) return @"iPad Air";
if ([deviceModel isEqualToString:@"iPad4,3"]) return @"iPad Air";
if ([deviceModel isEqualToString:@"iPad5,3"]) return @"iPad Air 2";
if ([deviceModel isEqualToString:@"iPad5,4"]) return @"iPad Air 2";
if ([deviceModel isEqualToString:@"iPad6,3"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"iPad6,4"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"iPad6,7"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"iPad6,8"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"iPad7,1"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"iPad7,2"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"iPad7,3"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"iPad7,4"]) return @"iPad Pro";
if ([deviceModel isEqualToString:@"i386"]) return @"Simulator";
if ([deviceModel isEqualToString:@"x86_64"]) return @"Simulator";
return deviceModel;
}