iOS如何在應用中連接WIFI
1、打開Hotspot Configuration權(quán)限光坝。
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);
}];
如何在應用中獲取已經(jīng)連接的WIFI SSID
1咨油、iOS14以上版本,獲取當前系統(tǒng)已經(jīng)連接的WiFi SSID可通過以下方法
NEHotspotConfigurationManager* manager = [NEHotspotConfigurationManager sharedManager];
[NEHotspotNetwork fetchCurrentWithCompletionHandler:^(NEHotspotNetwork * _Nullable currentNetwork) {
NSLog(@"current :%@", currentNetwork.SSID);
//打印結(jié)果: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“權(quán)限后才能獲取已經(jīng)連接的WiFi SSID棉胀。注意:該權(quán)限只有付費帳號才能打開法瑟。
3、iOS13系統(tǒng)對WiFi SSID獲取又進行了限制唁奢,需要開啟定位服務后才能獲取已經(jīng)連接的WiFi SSID霎挟。打開定位權(quán)限如下:
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
4、iOS14系統(tǒng)及以上麻掸,需要開啟精準定位服務后才能獲取已經(jīng)連接的WiFi SSID酥夭。獲取精準定位的方法如下:
self.manager = [[CLLocationManager alloc] init];
BOOL isFullAccuracy = (self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy);
if(!isFullAccuracy){
[self.manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"WantsToGetWiFiSSID"];
}