获取IP地址

//需导入的头文件

#import 

#include 

#include 

#include 

#define IOS_CELLULAR    @"pdp_ip0"

#define IOS_WIFI        @"en0"

#define IP_ADDR_IPv4    @"ipv4"

#define IP_ADDR_IPv6    @"ipv6"

//.h

+ (NSString *)getIPAddress;

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

+ (NSDictionary *)getIPAddresses;

//.m

+ (NSString *)getIPAddress {

[self getIPAddress:YES];

}

+ (NSString *)getIPAddress:(BOOL)preferIPv4

{

NSArray *searchArray = preferIPv4 ?

@[ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :

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

if(address) *stop = YES;

} ];

return address ? address : @"0.0.0.0";

}

+ (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;

if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {

NSString *name = [NSString stringWithUTF8String:interface->ifa_name];

char addrBuf[INET6_ADDRSTRLEN];

if(inet_ntop(addr->sin_family, &addr->sin_addr, addrBuf, sizeof(addrBuf))) {

NSString *key = [NSString stringWithFormat:@"%@/%@", name, addr->sin_family == AF_INET ? IP_ADDR_IPv4 : IP_ADDR_IPv6];

addresses[key] = [NSString stringWithUTF8String:addrBuf];

}

}

}

// Free memory

freeifaddrs(interfaces);

}

// The dictionary keys have the form "interface" "/" "ipv4 or ipv6"

return [addresses count] ? addresses : nil;

} 

谢谢阅读!

你可能感兴趣的:(获取IP地址)