iOS开发 ☞ 获取本机ip地址
来源:互联网 发布:c语言小明摘苹果 编辑:程序博客网 时间:2024/05/17 06:04
有些支付的第三方是要用到下面这段代码的,顺便吐槽一下名气越大的支付SDK,封装的越LOW,我没有说微信。。。。。
一些封装的比较好的SDK像极光推送,也难怪人家过了C轮即将上市。
//iPv4
- (NSString *)getIpAddresses{ 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;}
//ipv6
NSString *address = @"an error occurred when obtaining ip address"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; struct sockaddr_in *s4; struct sockaddr_in6 *s6; char buf[64]; int success = 0; Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"]; NetworkStatus netStatus = [reach currentReachabilityStatus]; //pdp_ip0 手机网络ip地址, en0 wifi 网络地址 NSString *netType = @"en0"; if (netStatus == ReachableViaWWAN) { netType = @"pdp_ip0"; } // 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:netType])//en1 on simulator if mac on wifi { s4 = (struct sockaddr_in *)temp_addr->ifa_addr; if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) == NULL) { } else{ address = [NSString stringWithUTF8String:buf]; } } } else if (temp_addr->ifa_addr->sa_family == AF_INET6) { // Check if interface is en0 which is the wifi connection on the iPhone if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:netType]) { s6 = (struct sockaddr_in6 *)(temp_addr->ifa_addr); if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) == NULL) { } else{ address = [NSString stringWithUTF8String:buf]; } } } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); NSLog(@"手机的IP是:%@", address); return address;
1 0
- iOS开发 ☞ 获取本机ip地址
- ios 获取本机IP地址
- iOS获取本机IP地址方式
- iOS项目开发实战——获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机ip地址
- 获取本机IP地址
- 获取本机ip地址
- 获取本机ip地址
- 获取本机IP地址
- CSS框模型
- final关键字
- Content Provider
- iOS使用Sencha Touch、ExtJs6 Modern或iscroll等框架时,文本框聚焦的光标不跟随页面(文本框)滚动 的解决办法
- 字符串转16进制的问题。在线等。急
- iOS开发 ☞ 获取本机ip地址
- js 基础
- 传智播客:俩小时助你解决网站排名!
- XML DOM完全参考手册(一)DOM与节点
- 设计模式6大原则
- CSS3之变形transform
- I,P,B帧和PTS,DTS的关系,GOP相关
- Collection Map 集合类
- 适配器模式