iOS 获取IP地址方法

2021-01-27  本文已影响0人  泪无痕521

获取ip地址

import <ifaddrs.h>

import <arpa/inet.h>

-(void)getIPaddress
{
NSString *address = @"error";
struct ifaddrs * ifaddress = NULL;
struct ifaddrs * temp_address = NULL;
int success = 0;
success = getifaddrs(&ifaddress);
if(success == 0) {
temp_address = ifaddress;
while(temp_address != NULL) {
if(temp_address->ifa_addr->sa_family == AF_INET) {
if([[NSString stringWithUTF8String:temp_address->ifa_name] isEqualToString:@"en0"]) {
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_address->ifa_addr)->sin_addr)];
}
}
temp_address = temp_address->ifa_next;
}
}

NSLog(@"获取到的IP地址为:%@",address);

}

上一篇 下一篇

猜你喜欢

热点阅读