在iOS 12中无法获取WiFi的SSID了?别慌!

栏目: IOS · 发布时间: 6年前

内容简介:Apple于北京时间9月17日晚在官网发布了敲黑板:获取WiFi的SSID是智能硬件App中配网模块的常用功能,各大厂商要针对iOS 12适配一波了(@huawei.com; @mi.com; @360.com; @jd.com; @baidu.com; @orvibo.com; @scinan.com; ...)。获取WiFi的SSID的方法如下:

Apple于北京时间9月17日晚在官网发布了 Xcode 10正式版 ,QiShare团队成员于9月18日中午统一升级以对现有项目做适配。期间,作者发现某项目使用Xcode 10编译出的App在iOS 12中无法获取WiFi的SSID。

敲黑板:获取WiFi的SSID是智能硬件App中配网模块的常用功能,各大厂商要针对iOS 12适配一波了(@huawei.com; @mi.com; @360.com; @jd.com; @baidu.com; @orvibo.com; @scinan.com; ...)。

获取WiFi的SSID的方法如下:

+ (NSString *)wifiSSID {    
   NSString *ssid = nil;    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();    for (NSString *ifnam in ifs) {        NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);        if (info[@"SSID"]) {
           ssid = info[@"SSID"];
       }
   }    return ssid;
}

在iOS 12中,经断点调试发现info为nil。猜测是CNCopyCurrentNetworkInfo方法在iOS 12中有了变化。通过查看官方文档,发现文档中增加了Important字样的描述:

在iOS 12中无法获取WiFi的SSID了?别慌!

CNCopyCurrentNetworkInfo文档

Important
To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App ID.
重要描述
在iOS 12+中使用此方法需要在Xcode中为应用授权获取WiFi信息的能力。授权后,Xcode会自动在App ID和应用的权限列表中增加获取WiFi信息的权限。

看到这里,解决方案就有了:Xcode -> [Project Name] -> Targets -> [Target Name] -> Capabilities -> Access WiFi Information -> ON

在iOS 12中无法获取WiFi的SSID了?别慌!

解决方案步骤图示

按照上述方案操作完毕后,会发现工程的.entitlements文件中多了一对Key-Value:

在iOS 12中无法获取WiFi的SSID了?别慌!

.entitlements文件

至此,就能在iOS 12+中正常获取到WiFi的SSID了。

作者: Xs·H

审校: QiShare团队

链接: https://www.jianshu.com/p/f96c55ffc5fd


以上所述就是小编给大家介绍的《在iOS 12中无法获取WiFi的SSID了?别慌!》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Machine Learning in Action

Machine Learning in Action

Peter Harrington / Manning Publications / 2012-4-19 / GBP 29.99

It's been said that data is the new "dirt"—the raw material from which and on which you build the structures of the modern world. And like dirt, data can seem like a limitless, undifferentiated mass. ......一起来看看 《Machine Learning in Action》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具