博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios获取ip地址
阅读量:4452 次
发布时间:2019-06-07

本文共 1556 字,大约阅读时间需要 5 分钟。

进来接微信支付,后台要传ip地址,so~

首先要导这些头文件

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include

然后就是方法啦:

1 + (NSString *)getIPAddress 2 { 3     NSString *address = @"error"; 4     struct ifaddrs *interfaces = NULL; 5     struct ifaddrs *temp_addr = NULL; 6     int success = 0; 7     // retrieve the current interfaces - returns 0 on success 8     success = getifaddrs(&interfaces); 9     if (success == 0)10     {11         // Loop through linked list of interfaces12         temp_addr = interfaces;13         while(temp_addr != NULL)14         {15             if(temp_addr->ifa_addr->sa_family == AF_INET)16             {17                 // Check if interface is en0 which is the wifi connection on the iPhone18                 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])19                 {20                     // Get NSString from C String21                     address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];22                 }23             }24             temp_addr = temp_addr->ifa_next;25         }26     }27     // Free memory28     freeifaddrs(interfaces);29     return address;30 }

我是给放在工具类里面了,哪里要用调哪里~so easy~

转载于:https://www.cnblogs.com/forhonour/p/3566128.html

你可能感兴趣的文章
angularjs 使用angular-sortable-view实现拖拽效果(包括拖动完成后的方法使用)
查看>>
2015生命之旅---南京、南通、上海之行
查看>>
高精度练习之乘法(codevs_3117)
查看>>
小Z爱划水
查看>>
Qt Font
查看>>
2014年生日
查看>>
扫描目录下的文件并拼接在一起
查看>>
ELK 分布式日志处理 10.12
查看>>
Java虚拟机详解05----垃圾收集器及GC参数
查看>>
7. 单位,移动布局
查看>>
inux中bin与sbin目录的作用及区别介绍
查看>>
USACO 3.1 Contact
查看>>
Office之什么是高内聚低耦合
查看>>
一些奇怪的问题求回答
查看>>
这些年踩过的坑
查看>>
iOS开发拓展篇——如何把项目托管到GitHub
查看>>
性能优化之数据库优化
查看>>
类的继承、菱形继承、派生、多态
查看>>
mysql约束
查看>>
javascript鼠标及键盘事件总结及案例
查看>>