ios 设备基本信息检测
开发ios确实会让人身心愉悦(相对于deskop,android),ios app更多的让人集中注意力到它本身的体验,性能。这非常好,我非常喜欢相对完美的事物。
最近遇到一些乱七八糟的需求。需要获取一些设备信息,网络情况。iOS 上开发非常简单有趣,除了一些乱七八糟的信息,例如:摄像头硬件信息,本地相册初始化时间(第一次拍照保存),等。
剩下一些信息就是我要的了:
还是代码演示:
#define IOS_CELLULAR @"pdp_ip0" #define IOS_WIFI @"en0" #define IP_ADDR_IPv4 @"ipv4" #define IP_ADDR_IPv6 @"ipv6" #define ADDRESSIP @"http://www.cz88.net/ip/viewip778.aspx" #define ADDRESSIPWY @"http://nstool.netease.com/"
//获取本地的DNS IP + (NSDictionary *)getDNSIp { res_state res = (res_state)malloc(sizeof(struct __res_state)); __uint32_t dwDNSIP = 0; int result = res_ninit(res); if (result == 0) { dwDNSIP = res->nsaddr_list[0].sin_addr.s_addr; } free(res); NSString *dns = [NSString stringWithUTF8String:inet_ntoa(res->nsaddr_list[0].sin_addr)]; NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: dns, @"LocalDNS", nil]; return tmp; }
//获取制定域名的IP地址 + (NSDictionary *)ServersToIp:(NSString *)ip { Boolean result; CFHostRef hostRef; CFArrayRef addresses=nil; NSString *hostname = ip; NSMutableArray *array = [[NSMutableArray alloc] init]; hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostname); if (hostRef) { result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed if (result == TRUE) { addresses = CFHostGetAddressing(hostRef, &result); } } if (result == TRUE) { NSMutableArray *tempDNS = [[NSMutableArray alloc] init]; for(int i = 0; i < CFArrayGetCount(addresses); i++){ struct sockaddr_in* remoteAddr; CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i); remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData); if(remoteAddr != NULL){ // Extract the ip address //const char *strIP41 = inet_ntoa(remoteAddr->sin_addr); NSString *strDNS =[NSString stringWithCString:inet_ntoa(remoteAddr->sin_addr) encoding:NSASCIIStringEncoding]; [array addObject:strDNS]; [tempDNS addObject:strDNS]; } } } else { NSLog(@"Not resolved"); } return [NSDictionary dictionaryWithObjectsAndKeys: array, ip, nil]; }
/*第三方获取外网ip*/
//全真ip + (NSDictionary*)getWanIPAddress { NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; NSMutableArray *ip_array = [[NSMutableArray alloc] init]; NSMutableArray *pos_array = [[NSMutableArray alloc] init]; NSURLRequest *request = [NSMutableURLRequest requestWithURL:[[NSURL alloc]initWithString: ADDRESSIP] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; if (error == nil) { NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString *responseStr = [[NSString alloc] initWithData:data encoding:enc]; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" options:NSRegularExpressionCaseInsensitive error:&error]; if (error == nil) { NSArray *matches = [regex matchesInString:responseStr options:0 range:NSMakeRange(0, [responseStr length])]; for (NSTextCheckingResult *match in matches) { [ip_array addObject:[responseStr substringWithRange:match.range]]; // NSLog(@"the ip address:%@", [responseStr substringWithRange:match.range]); } } regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=AddrMessage\">).*(?=</spa)" options:NSRegularExpressionCaseInsensitive error:&error]; if (error == nil) { NSArray *matches = [regex matchesInString:responseStr options:0 range:NSMakeRange(0, [responseStr length])]; for (NSTextCheckingResult *match in matches) { [pos_array addObject:[responseStr substringWithRange:match.range]]; // NSLog(@"设备位置:%@", [responseStr substringWithRange:match.range]); } } [dic setValue:ip_array forKey:@"ip"]; [dic setValue:pos_array forKey:@"positon"]; } else { [dic setValue:error forKey:@"get error"]; } return dic; } //网易ip + (NSDictionary*)getWanIPAddressWY { NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; NSMutableArray *ip_array = [[NSMutableArray alloc] init]; NSMutableArray *dns_array = [[NSMutableArray alloc] init]; NSURLRequest *request = [NSMutableURLRequest requestWithURL:[[NSURL alloc]initWithString: ADDRESSIPWY] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; if (error == nil) { NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString *responseStr = [[NSString alloc] initWithData:data encoding:enc]; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=<iframe src=‘).*(?=‘ frameborder)" options:NSRegularExpressionCaseInsensitive error:&error]; if (error == nil) { NSArray *matches = [regex matchesInString:responseStr options:0 range:NSMakeRange(0, [responseStr length])]; if ([matches count] != 1) { [dic setValue:@"count != 1" forKey:@"get error"]; return dic; } for (NSTextCheckingResult *match in matches) { request = [NSMutableURLRequest requestWithURL:[[NSURL alloc]initWithString: [responseStr substringWithRange:match.range]] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; if (error == nil) { NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString *resStr = [[NSString alloc] initWithData:data encoding:enc]; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"您的IP地址信息:.*?(?=<br>)" options:NSRegularExpressionCaseInsensitive error:&error]; if (error == nil) { NSArray *matches = [regex matchesInString:resStr options:0 range:NSMakeRange(0, [resStr length])]; for (NSTextCheckingResult *match in matches) { [ip_array addObject:[resStr substringWithRange:match.range]]; // NSLog(@"ip msg:%@", [resStr substringWithRange:match.range]); } } regex = [NSRegularExpression regularExpressionWithPattern:@"您的DNS地址信息:.*?(?=<br>)" options:NSRegularExpressionCaseInsensitive error:&error]; if (error == nil) { NSArray *matches = [regex matchesInString:resStr options:0 range:NSMakeRange(0, [resStr length])]; for (NSTextCheckingResult *match in matches) { [dns_array addObject:[resStr substringWithRange:match.range]]; // NSLog(@"DNS msg:%@", [resStr substringWithRange:match.range]); } } [dic setValue:ip_array forKey:@"ip"]; [dic setValue:dns_array forKey:@"positon"]; } } } else { [dic setValue:@"count != 1" forKey:@"get error"]; } } else { [dic setValue:@"count != 1" forKey:@"get error"]; } return dic; }
#pragma mark - deviceMsg //idfa idfv 基本的设备信息 - (void)getDevMsg { UIDevice *device=[[UIDevice alloc] init]; NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; }
#pragma mark - wifiStatus //获取Wi-Fi信息(如果链接了wifi) - (void)detectWifi{ NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); id info = nil; for (NSString *ifnam in ifs) { info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); if(info != nil){ //TODU } } [self finishStatus:@"wifi"]; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。