github地址:
https://github.com/42vio/iOS-NetworkExtension-NEHotspotHelper
?NOTE
- 應(yīng)用程序的Info.plist必須添加一個(gè)包含“網(wǎng)絡(luò)認(rèn)證”的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或點(diǎn)擊申請(qǐng)鏈接https://developer.apple.com/contact/network-extension/
- 更多信息請(qǐng)參閱蘋(píng)果的Hotspot Network Subsystem Programming Guide
NEHotsportHelper Custom authentication for Wi-Fi Hotspots
Register with the system as a Hotspot Helper
From the backgroud
- Claim Hotspots with a level of confidence
- Perform the initial authentication
- Maintain the authentication session
- Annotate Wifi networks in the WiFi network scanner
Register a Hotspot Helper
+ (BOOL)registerWithOptions:(NSDictionary<NSString *, NSObject *> *)options
queue:(dispatch_queue_t)queue
handler:(NEHotspotHelperHandler)handler
@param options
<key>kNEHotspotHelperOptionDisplayName</key>
<string>WIFI的注釋tag字符串</string>
@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砂豌。
Manage Hotspot Networks
+ (BOOL)logoff:(NEHotspotNetwork *)network
@param network
對(duì)應(yīng)當(dāng)前關(guān)聯(lián)的WiFi網(wǎng)絡(luò)NEHotspotNetwork
@return 注銷(xiāo)命令已成功進(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ò)。
Data Types
typedef void (^NEHotspotHelperHandler)(NEHotspotHelperCommand * cmd)
@discussion
當(dāng)調(diào)用方法registerWithOptions:queue:handler:時(shí)重罪,Hotspot Helper app提供這種類(lèi)型的blcok樱哼。
每次有要處理的命令時(shí)調(diào)用block哀九。
實(shí)現(xiàn)代碼
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:@"WI-FI TAG", kNEHotspotHelperOptionDisplayName, nil];
dispatch_queue_t queue = dispatch_queue_create("com.myapp.ex", 0);
BOOL returnType = [NEHotspotHelper registerWithOptions:options queue:queue handler: ^(NEHotspotHelperCommand * cmd) {
if(cmd.commandType == kNEHotspotHelperCommandTypeEvaluate ||
cmd.commandType == kNEHotspotHelperCommandTypeFilterScanList)
{
for (NEHotspotNetwork* network in cmd.networkList)
{
if ([network.SSID isEqualToString:@"WX_moses"])
{
[network setConfidence:kNEHotspotHelperConfidenceHigh];
[network setPassword:@"mypassword"];
NSLog(@"Confidence set to high for ssid: %@ (%@)\n\n", network.SSID, network.BSSID);
// NSMutableArray *hotspotList = [NSMutableArray new];
// [hotspotList addObject:network];
// This is required
NEHotspotHelperResponse *response = [cmd createResponse:kNEHotspotHelperResultSuccess];
[response setNetwork:network];
[response deliver];
}
}
}
}];
? 獲得失敗時(shí)可能需要以下設(shè)置
- 創(chuàng)建項(xiàng)目 App id 時(shí)配置 App Services - Wireless Accessory Configuration
- 在項(xiàng)目中配置 plist 文件
<key>UIBackgroundModels</key>
<array>
<string>network-authentication</string>
</array>
- 在項(xiàng)目的 entitlements 后綴的文件中添加
com.apple.developer.networking.HotspotHelper 權(quán)利 - 要進(jìn)入到【無(wú)線局域網(wǎng)】設(shè)置頁(yè)面才能獲取 wifi 列表