https://www.jianshu.com/p/0135e7db5a69
iOS如何在应用中连接WIFI
1、打开Hotspot Configuration权限。
2、在info.plist中添加"Required background modes",在item0赋值为"network-authentication"。注意,当添加完成后,App会打开"Background Modes"能力。
3、导入头文件"#import <NetworkExtension/NetworkExtension.h>"。
4、通过代码连接指定SSID的WIFI。
NEHotspotConfiguration* config = [[NEHotspotConfiguration alloc] initWithSSID:@"WIFI SSID" passphrase:@"WIFI 密码" isWEP:NO];
NEHotspotConfigurationManager* manager = [NEHotspotConfigurationManager sharedManager];
[manager applyConfiguration:config completionHandler:^(NSError * _Nullable error) {
NSLog(@"%@",error);
}];
如何在应用中获取已经连接的WIFI SSID
1、iOS14以上版本,获取当前系统已经连接的WiFi SSID可通过以下方法
NEHotspotConfigurationManager* manager = [NEHotspotConfigurationManager sharedManager];
[NEHotspotNetwork fetchCurrentWithCompletionHandler:^(NEHotspotNetwork * _Nullable currentNetwork) {
NSLog(@"current :%@", currentNetwork.SSID);
//打印结果:current :UKeeper-5G
}];
iOS14以下,可以使用以下方式获取当前连接的WiFi SSID
+ (NSString*)wifSsid{
NSArray *interfaces = (__bridge_transfer NSArray*)CNCopySupportedInterfaces();
NSDictionary *info = nil;
for (NSString *ifname in interfaces) {
info = (__bridge_transfer NSDictionary*)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
if (info && [info count]) {
break;
}
info = nil;
}
NSString *ssid = nil;
if ( info ){
ssid = [info objectForKey:@"SSID"];
}
info = nil;
return ssid? ssid:@"";
}
2、由于iOS12对获取WiFi做了限制,App需要打开”Access WiFi Infomation“权限后才能获取已经连接的WiFi SSID。注意:该权限只有付费帐号才能打开。
3、iOS13系统对WiFi SSID获取又进行了限制,需要开启定位服务后才能获取已经连接的WiFi SSID。打开定位权限如下:
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
4、iOS14系统及以上,需要开启精准定位服务后才能获取已经连接的WiFi SSID。获取精准定位的方法如下:
self.manager = [[CLLocationManager alloc] init];
BOOL isFullAccuracy = (self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy);
if(!isFullAccuracy){
[self.manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"WantsToGetWiFiSSID"];
}
1人点赞
随笔
作者:卡布奇诺_95d2
链接:https://www.jianshu.com/p/0135e7db5a69
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 标签:info,WIFI,SSID,WiFi,iOS,获取,manager From: https://www.cnblogs.com/itlover2013/p/17258736.html