#import <ifaddrs.h>#import <arpa/inet.h>// Get ip Address- (NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // retrieve the current interfaces - returns 0 on success success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked list of interfaces temp_addr = interfaces; while(temp_addr != NULL) { if(temp_addr->ifa_addr->sa_family == AF_INET) { // Check if interface is en0 which is the wifi connection on the iPhone if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // Get NSString from C String address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); return address;}1.已禁用-[UIDevice uniqueIdentifier]
蘋(píng)果總是把用戶(hù)的隱私看的很重要。-[UIDevice uniqueIdentifier]在iOS5實(shí)際在iOS5的時(shí)候已經(jīng)被遺棄了,但是iOS7中已經(jīng)完全的禁用了它。Xcode5甚至不會(huì)允許你編譯包含了指引到-[UIDevice uniqueIdentifier]的app。此外,iOS7之前的使用了-[UIDevice uniqueIdentifier] 的app如果在iOS7上運(yùn)行,它不會(huì)返回設(shè)備的UUID,而是會(huì)返回一串字符串,以FFFFFFFF開(kāi)頭,跟著-[UIDevice identifierForVendor]的十六進(jìn)制值。
現(xiàn)在蘋(píng)果明確的表明你應(yīng)該使用-[UIDevice identifierForVendor]或是-[ASIdentifierManager advertisingIdentifier]來(lái)作為你框架和應(yīng)用的唯一標(biāo)示符。坦白的來(lái)說(shuō),應(yīng)對(duì)這些變化也不是那么的難,見(jiàn)以下代碼片段:
NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString]; NSString *identifierForAdvertising = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];每種方法都適配一種特別的用法:
identifierForVendor對(duì)供應(yīng)商來(lái)說(shuō)是唯一的一個(gè)值,也就是說(shuō),由同一個(gè)公司發(fā)行的的app在相同的設(shè)備上運(yùn)行的時(shí)候都會(huì)有這個(gè)相同的標(biāo)識(shí)符。然而,如果用戶(hù)刪除了這個(gè)供應(yīng)商的app然后再重新安裝的話,這個(gè)標(biāo)識(shí)符就會(huì)不一致。
advertisingIdentifier會(huì)返回給在這個(gè)設(shè)備上所有軟件供應(yīng)商相同的 一個(gè)值,所以只能在廣告的時(shí)候使用。這個(gè)值會(huì)因?yàn)楹芏嗲闆r而有所變化,比如說(shuō)用戶(hù)初始化設(shè)備的時(shí)候便會(huì)改變。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注