iOS 获取客户端ip
2016-03-24 20:43
344 查看
NSString *address =
@"an error occurred when obtaining ip address";
struct
ifaddrs *interfaces =
NULL;
struct
ifaddrs *temp_addr =
NULL;
int success =
0;
success = getifaddrs(&interfaces);
if (success ==
0) { // 0
表示获取成功
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;
}
}
freeifaddrs(interfaces);
NSLog(@"手机的IP是:%@",
address);
@"an error occurred when obtaining ip address";
struct
ifaddrs *interfaces =
NULL;
struct
ifaddrs *temp_addr =
NULL;
int success =
0;
success = getifaddrs(&interfaces);
if (success ==
0) { // 0
表示获取成功
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;
}
}
freeifaddrs(interfaces);
NSLog(@"手机的IP是:%@",
address);
相关文章推荐
- iOS定位CLLocationManager以及CLLocationManagerDelegate协议的分享
- IOS开发之滤镜 CIImage、CIFilter
- iOS 检测版本升级比较好用的第三方类库Harpy的简单使用
- 我的ios 开发学习之旅!
- iOS缓存清理
- IPHONE手机进入DFU模式刷 IOS
- iOS个人整理32-多线程NSThread NSOperation
- 微博API iOS9的适配问题
- 联想lenovo ThinkPad笔记本电脑开机进入BIOS的方法
- iOS键盘弹起,上移视图,和其它的方法大同小异,只不过我在viewDidLayoutSubviews 中写的改变frame方法,这样就不用监听键盘什么的了
- iOS-时间与日期详解
- iOS 批量下载-效果图
- iOS技术之批量下载
- 关于iOS spritekit学习
- iOS关于NSCharacterSet 的用法
- iOS 真机Could not find Developer Disk Image
- iOS —— Attributes文字的操作,TextFiel框内文字颜色大小等改变
- 修改状态栏的字体颜色
- iOS block传值的用法
- 写出方法获取ios内存使用情况