9.0以前獲取需要引入#import<SystemConfiguration/CaptiveNetwork.h>
+ (NSString *)getWifiName{
NSString *wifiName = nil;
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
if (!wifiInterfaces) {
return @"未知";
}
NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
for (NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
CFRelease(dictRef);
}
}
CFRelease(wifiInterfaces);
return wifiName;
}
9.0以后獲取需要引入import <NetworkExtension/NetworkExtension.h>
但在應(yīng)用中需要添加icloud containers
NSArray * networkInterfaces = [NEHotspotHelper supportedNetworkInterfaces];
NSLog(@"Networks %@",networkInterfaces);
//獲取wifi列表
for(NEHotspotNetwork *hotspotNetwork in [NEHotspotHelper supportedNetworkInterfaces]) {
NSString *ssid = hotspotNetwork.SSID;
NSString *bssid = hotspotNetwork.BSSID;
BOOL secure = hotspotNetwork.secure;
BOOL autoJoined = hotspotNetwork.autoJoined;
double signalStrength = hotspotNetwork.signalStrength;}
應(yīng)用程序的Info.plist必須添加一個包含“remote-notification”的UIBackgroundModes數(shù)組.
應(yīng)用程序必須設(shè)置“com.apple.developer.networking.HotspotHelper'作為它的權(quán)利之一抄邀。
該權(quán)利的值是一個布爾值true要申請這個權(quán)利,請發(fā)送E-MAIL到networkextension@apple.com更多信息請參閱蘋果的Hotspot Network Subsystem Programming Guide
申請鏈接 :https://developer.apple.com/contact/network-extension/
詳閱官方文檔: https://developer.apple.com/reference/networkextension/nehotspothelper
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/Hotspot_Network_Subsystem_Guide/Contents/Introduction.html#//apple_ref/doc/uid/TP40016639
+ (BOOL)registerWithOptions:(NSDictionary*)options queue:(dispatch_queue_t)queue handler:(NEHotspotHelperHandler)handler
@param options
kNEHotspotHelperOptionDisplayName :WIFI的注釋tag字符串// 此處設(shè)置的內(nèi)容會在WiFi列表中每個WiFi下邊展示出來
@param queue dispatch_queue_t 用來調(diào)用handle的block
@param handler NEHotspotHelperHandler block 用于執(zhí)行處理 helper commands.
@return 注冊成功YES, 否則NO.
@discussion 一旦這個API調(diào)用成功,應(yīng)用程序有資格在后臺啟動托酸,并參與各種熱點相關(guān)的功能各淀。 當應(yīng)用程序啟動此方法應(yīng)該調(diào)用一次醉箕。再次調(diào)用它會不會產(chǎn)生影響羽德,并返回NO。
+ (BOOL)logoff:(NEHotspotNetwork *)network
@param network 對應(yīng)當前關(guān)聯(lián)的WiFi網(wǎng)絡(luò)NEHotspotNetwork
@return 注銷命令已成功進入隊列YES, 否則NO.
@discussion 調(diào)用此方法使kNEHotspotHelperCommandTypeLogoff型的NEHotspotHelperCommand向應(yīng)用程序發(fā)出的“handler”模塊 網(wǎng)絡(luò)參數(shù)必須符合當前關(guān)聯(lián)的WiFi網(wǎng)絡(luò)神郊,即它必須來自對NEHotspotHelperCommand網(wǎng)絡(luò)屬性或方法supportedInterfaces
+ (NSArray *)supportedNetworkInterfaces
@return 如果沒有網(wǎng)絡(luò)接口被管理肴裙,返回nil。否則涌乳,返回NEHotspotNetwork對象數(shù)組蜻懦。
@discussion 每個網(wǎng)絡(luò)接口由NEHotspotNetwork對象表示。當前返回的數(shù)組包含一個NEHotspotNetwork對象代表Wi-Fi接口夕晓。
這種方法的主要目的是當沒有得到一個命令來處理它時宛乃,讓一個熱點助手偶爾提供在UI里其準確的狀態(tài)。 此方法加上NEHotspotNetwork的isChosenHelper方法允許應(yīng)用程序知道它是否是當前處理的網(wǎng)絡(luò)蒸辆。
NSMutableDictionary* options = [[NSMutableDictionary alloc] init];
[options setObject:@"我是副標題"?forKey:kNEHotspotHelperOptionDisplayName];
dispatch_queue_t queue = dispatch_queue_create("com.myapp.ex", NULL);
BOOL returnType = [NEHotspotHelper registerWithOptions:options queue:queue handler: ^(NEHotspotHelperCommand * cmd) {
NEHotspotNetwork* network;
NSLog(@"COMMAND TYPE: ? %ld", (long)cmd.commandType);
[cmd createResponse:kNEHotspotHelperResultAuthenticationRequired];
if (cmd.commandType == kNEHotspotHelperCommandTypeEvaluate || cmd.commandType ==kNEHotspotHelperCommandTypeFilterScanList) {
NSLog(@"WIFILIST: ? %@", cmd.networkList);
for (network? in cmd.networkList) {
NSLog(@"COMMAND TYPE After: ? %ld", (long)cmd.commandType);
if ([network.SSID isEqualToString:@"ssid"]|| [network.SSID isEqualToString:@"WISPr Hotspot"]) {
double signalStrength = network.signalStrength;
NSLog(@"Signal Strength: %f", signalStrength);
[network setConfidence:kNEHotspotHelperConfidenceHigh];
[network setPassword:@"password"];
NEHotspotHelperResponse *response = [cmd createResponse:kNEHotspotHelperResultSuccess];
NSLog(@"Response CMD %@", response);
[response setNetworkList:@[network]];
[response setNetwork:network];
[response deliver];
}
}
}
}];
NSLog(@"result :%d", returnType);
NSArray *array = [NEHotspotHelper supportedNetworkInterfaces];
NSLog(@"wifiArray:%@", array);
NEHotspotNetwork *connectedNetwork = [array lastObject];
NSLog(@"supported Network Interface: %@", connectedNetwork);