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必須添加一個(gè)包含“remote-notification”的UIBackgroundModes數(shù)組.
應(yīng)用程序必須設(shè)置“com.apple.developer.networking.HotspotHelper'作為它的權(quán)利之一。
該權(quán)利的值是一個(gè)布爾值true要申請(qǐng)這個(gè)權(quán)利,請(qǐng)發(fā)送E-MAIL到networkextension@apple.com更多信息請(qǐng)參閱蘋果的Hotspot Network Subsystem Programming Guide
申請(qǐng)鏈接 :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)容會(huì)在WiFi列表中每個(gè)WiFi下邊展示出來(lái)
@param queue dispatch_queue_t 用來(lái)調(diào)用handle的block
@param handler NEHotspotHelperHandler block 用于執(zhí)行處理 helper commands.
@return 注冊(cè)成功YES, 否則NO.
@discussion 一旦這個(gè)API調(diào)用成功,應(yīng)用程序有資格在后臺(tái)啟動(dòng),并參與各種熱點(diǎn)相關(guān)的功能。 當(dāng)應(yīng)用程序啟動(dòng)此方法應(yīng)該調(diào)用一次。再次調(diào)用它會(huì)不會(huì)產(chǎn)生影響,并返回NO。
+ (BOOL)logoff:(NEHotspotNetwork *)network
@param network 對(duì)應(yīng)當(dāng)前關(guān)聯(lián)的WiFi網(wǎng)絡(luò)NEHotspotNetwork
@return 注銷命令已成功進(jìn)入隊(duì)列YES, 否則NO.
@discussion 調(diào)用此方法使kNEHotspotHelperCommandTypeLogoff型的NEHotspotHelperCommand向應(yīng)用程序發(fā)出的“handler”模塊 網(wǎng)絡(luò)參數(shù)必須符合當(dāng)前關(guān)聯(lián)的WiFi網(wǎng)絡(luò),即它必須來(lái)自對(duì)NEHotspotHelperCommand網(wǎng)絡(luò)屬性或方法supportedInterfaces
+ (NSArray *)supportedNetworkInterfaces
@return 如果沒(méi)有網(wǎng)絡(luò)接口被管理,返回nil。否則,返回NEHotspotNetwork對(duì)象數(shù)組。
@discussion 每個(gè)網(wǎng)絡(luò)接口由NEHotspotNetwork對(duì)象表示。當(dāng)前返回的數(shù)組包含一個(gè)NEHotspotNetwork對(duì)象代表Wi-Fi接口。
這種方法的主要目的是當(dāng)沒(méi)有得到一個(gè)命令來(lái)處理它時(shí),讓一個(gè)熱點(diǎn)助手偶爾提供在UI里其準(zhǔn)確的狀態(tài)。 此方法加上NEHotspotNetwork的isChosenHelper方法允許應(yīng)用程序知道它是否是當(dāng)前處理的網(wǎng)絡(luò)。
NSMutableDictionary* options = [[NSMutableDictionary alloc] init];
[options setObject:@"我是副標(biāo)題"?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);