49

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

 5 years ago
source link: http://www.cocoachina.com/ios/20180921/24995.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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字样的描述:

EJbMFzu.png!web

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

IzM3uu6.png!web

解决方案步骤图示

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

Ez2uyej.png!web

.entitlements文件

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

作者: Xs·H

审校: QiShare团队

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK