ios 裝置基本資料檢測

來源:互聯網
上載者:User

標籤:

開發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"];}

ios 裝置基本資料檢測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.