博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 获取本地设备IP地址
阅读量:1853 次
发布时间:2019-04-26

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

#import 
#import
// Get IP Address- (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;}
1.已禁用-[UIDevice uniqueIdentifier]   苹果总是把用户的隐私看的很重要。-[UIDevice uniqueIdentifier]在iOS5实际在iOS5的时候已经被遗弃了,但是iOS7中已经完全的禁用了它。Xcode5甚至不会允许你编译包含了指引到-[UIDevice uniqueIdentifier]的app。此外,iOS7之前的使用了-[UIDevice uniqueIdentifier] 的app如果在iOS7上运行,它不会返回设备的UUID,而是会返回一串字符串,以FFFFFFFF开头,跟着-[UIDevice identifierForVendor]的十六进制值。 现在苹果明确的表明你应该使用-[UIDevice identifierForVendor]或是-[ASIdentifierManager advertisingIdentifier]来作为你框架和应用的唯一标示符。坦白的来说,应对这些变化也不是那么的难,见以下代码片段:   
NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString]; 
NSString *identifierForAdvertising = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString]; 
每种方法都适配一种特别的用法:   identifierForVendor对供应商来说是唯一的一个值,也就是说,由同一个公司发行的的app在相同的设备上运行的时候都会有这个相同的标识符。然而,如果用户删除了这个供应商的app然后再重新安装的话,这个标识符就会不一致。   advertisingIdentifier会返回给在这个设备上所有软件供应商相同的 一个值,所以只能在广告的时候使用。这个值会因为很多情况而有所变化,比如说用户初始化设备的时候便会改变。
 
 

转载地址:http://cwlyf.baihongyu.com/

你可能感兴趣的文章
树莓派安装WIringPi库Python版流程
查看>>
怎样把多个WEB项目部署到同一个Tomcat下(同一端口)
查看>>
Fragment中点击不同的RelativeLayout跳转到不同的Activity,会出现活动重叠的现象.
查看>>
在Visual Studio上运行JM8.6
查看>>
target/TestDemo-1.0-SNAPSHOT.jar中没有主清单属性(已解决)
查看>>
Failed to retrieve data from webhdfsv1op=LISTSTATUS: java.lang.reflect.InvocationTargetException
查看>>
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
查看>>
ubuntu部署flink集群 ,运行wordcount 例子
查看>>
MapReduce处理图片
查看>>
windows编写JAVA代码调用opencv打包到linux上运行
查看>>
Java中包的定义与使用
查看>>
在Android studio中设置按钮点击变色效果的方法
查看>>
从Fragment跳转到一个活动的问题
查看>>
WPA基础
查看>>
Golang 实现Windows服务
查看>>
MathNet.Numerics主要类功能简述
查看>>
QuPath script
查看>>
go-iris-websocket 简单聊天通信
查看>>
GoCV golang版opencv测试(Windows)
查看>>
Gocv+websocket实现视频直播
查看>>