java获取ios设备ip

编程入门 行业动态 更新时间:2024-10-12 05:48:29

java获取ios<a href=https://www.elefans.com/category/jswz/34/1769313.html style=设备ip"/>

java获取ios设备ip

一、获取公网ip地址的方法

-(NSString *)deviceWANIPAddress

{

NSURL *ipURL = [NSURL URLWithString:@".php?ip=myip"];

NSData *data = [NSData dataWithContentsOfURL:ipURL];

NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSString *ipStr = nil;

if (ipDic && [ipDic[@"code"] integerValue] == 0) { //获取成功

ipStr = ipDic[@"data"][@"ip"];

}

return (ipStr ? ipStr : @"");

}

这个ipDic里面的数据结构如下图,可以获取IP的所在地和国家id等等的信息

image.png

二、获取内网的私有IP地址

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

}

更多推荐

java获取ios设备ip

本文发布于:2024-02-07 02:21:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1752143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设备   java   ios   ip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!