//首先加入头文件
#include <ifaddrs.h>
#include <arpa/inet.h>
//获取本机IP
-(NSString *)getDeviceIPAdress
{
NSString *address = @"error";
structifaddrs *interfaces = NULL;
structifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0)
{
temp_addr = interfaces;
while (temp_addr != NULL)
{
if (temp_addr->ifa_addr->sa_family == AF_INET)
{
if ([[NSStringstringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
address = [NSStringstringWithUTF8String:inet_ntoa(((structsockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return address;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。