iOS 獲取手機(jī)的IP地址(內(nèi)網(wǎng)與外網(wǎng))

?聯(lián)系人:石虎 QQ:1224614774 昵稱:嗡嘛呢叭咪哄

??????????????????? QQ群:807236138 群稱:iOS 技術(shù)交流學(xué)習(xí)群

一、概念

1.先是獲取內(nèi)網(wǎng)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"

#pragma mark - 獲取設(shè)備當(dāng)前網(wǎng)絡(luò)IP地址

+ (NSString *)getIPAddress:(BOOL)preferIPv4{

NSArray *searchArray = preferIPv4 ?

@[ 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 ] :

@[ IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;

NSDictionary *addresses = [self getIPAddresses];

NSLog(@"addresses: %@", addresses);

__block NSString *address;

[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)

{

address = addresses[key];

//篩選出IP地址格式

if([self isValidatIP:address]) *stop = YES;

} ];

return address ? address : @"0.0.0.0";

}

+ (BOOL)isValidatIP:(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 = [NSRegularExpression regularExpressionWithPattern: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];

//輸出結(jié)果

NSLog(@"%@",result);

return YES;

}

}

return NO;

}

+ (NSDictionary *)getIPAddresses{

NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];

// retrieve the current interfaces - returns 0 on success

struct ifaddrs *interfaces;

if(!getifaddrs(&interfaces)) {

// Loop through linked list of interfaces

struct ifaddrs *interface;

for(interface=interfaces; interface; interface=interface->ifa_next) {

if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {

continue; // deeply nested code harder to read

}

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

}

}

}

// Free memory

freeifaddrs(interfaces);

}

return [addresses count] ? addresses : nil;

}


2.獲取外網(wǎng)IP的方法:

//方法一:

NSError *error;

NSURL *ipURL = [NSURL URLWithString:@"http://ifconfig.me/ip"];

NSString *ip = [NSString stringWithContentsOfURL:ipURL encoding:NSUTF8StringEncoding error:&error];

//方法二:個(gè)人推薦用這個(gè)請(qǐng)求,速度比較快

/*

http://ipof.in/json

http://ipof.in/xml

http://ipof.in/txt

If you want HTTPS you can use the same URLs with https prefix. The advantage being that even if you are on a Wifi you will get the public address.

*/

NSError *error;

NSURL *ipURL = [NSURL URLWithString:@"http://ipof.in/txt"];

NSString *ip = [NSString stringWithContentsOfURL:ipURL encoding:NSUTF8StringEncoding error:&error];


謝謝!!!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容