天天看點

iOS 9 NEHotspotHelper 的NetworkExtension使用

iOS 9 釋出之後,蘋果對于WiFi這塊廢棄了 CaptiveNetwork了這個類,不再推薦使用,同時推出 NetworkExtension。最近把對這個類的研究以及心得體會寫下來。 效果圖如下,可以在指定的WiFi下設定自定義的文字。

iOS 9 NEHotspotHelper 的NetworkExtension使用

   首先,要想使用這個類需要向蘋果( " networkexten[email protected]om ")發郵件擷取這個權限,這裡不做具體詳細說明,需要說明的一點是你必須說明你為什麼要使用這個類,蘋果會回複給你一個問卷文檔,根據你的實際需求進行填寫。網上說三周時間會給予回複,我們的公司可能回複的有點慢了。不過所幸最終通過了。 當你在郵件中看到這幾行話說明你成功了! Hi,   Thanks for your interest in the Network Extension APIs. We added a new template containing the Network Extension  entitlements to your team. 然後你就可以在你的項目中配置你的證書了。 這裡附上配置證書的指導文檔 < https://developer.apple.com/library/ios/technotes/tn2415/_index.html#//apple_ref/doc/uid/DTS40016427 > 如果配置完成,還可以通過<https://forums.developer.apple.com/message/75928#75928> 這個對你的配置進行檢測,看是否配置成功。 當然最最重要的還是對NetworkExtension這個類的運用,以下是對這個類的中文翻譯。

NOTE

  • 應用程式的Info.plist必須添加一個包含“remote-notification”的UIBackgroundModes數組
  • 應用程式必須設定“com.apple.developer.networking.HotspotHelper'作為它的權利之一。該權利的值是一個布爾值true
  • 要申請這個權利,請發送E-MAIL到[email protected]
  • 更多資訊請參閱蘋果的Hotspot Network Subsystem Programming Guide

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>  // 此處設定的内容會在WiFi清單中每個WiFi下邊展示出來

@param queue 
    dispatch_queue_t 用來調用handle的block

@param handler
    NEHotspotHelperHandler block 用于執行處理 helper commands.

@return 注冊成功YES, 否則NO.

@discussion
    一旦這個API調用成功,應用程式有資格在背景啟動,并參與各種熱點相關的功能。
    當應用程式啟動此方法應該調用一次。再次調用它會不會産生影響,并傳回NO。
           

Manage Hotspot Networks

+ (BOOL)logoff:(NEHotspotNetwork *)network
           
@param network 
    對應目前關聯的WiFi網絡NEHotspotNetwork

@return 登出指令已成功進入隊列YES, 否則NO.

@discussion
    調用此方法使kNEHotspotHelperCommandTypeLogoff型的NEHotspotHelperCommand向應用程式發出的“handler”子產品
    網絡參數必須符合目前關聯的WiFi網絡,即它必須來自對NEHotspotHelperCommand網絡屬性或方法supportedInterfaces
           
+ (NSArray *)supportedNetworkInterfaces
           
@return
    如果沒有網絡接口被管理,傳回nil。否則,傳回NEHotspotNetwork對象數組。

@discussion
    每個網絡接口由NEHotspotNetwork對象表示。目前傳回的數組包含一個NEHotspotNetwork對象代表Wi-Fi接口。
    這種方法的主要目的是當沒有得到一個指令來處理它時,讓一個熱點助手偶爾提供在UI裡其準确的狀态。
    此方法加上NEHotspotNetwork的isChosenHelper方法允許應用程式知道它是否是目前處理的網絡。
           

Data Types

typedef void (^NEHotspotHelperHandler)(NEHotspotHelperCommand * cmd)
           
@discussion
    當調用方法registerWithOptions:queue:handler:時,Hotspot Helper app提供這種類型的blcok。
    每次有要處理的指令時調用block。
           

以上為對這個類的一些解讀,最後附上具體的代碼。

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(@"cmd %@", cmd);

        NSLog(@"COMMAND TYPE:   %ld", (long)cmd.commandType);

        NSLog(@"network %@",cmd.network);

        NSLog(@"networkList %@",cmd.networkList);

        if (cmd.commandType == kNEHotspotHelperCommandTypeEvaluate || cmd.commandType ==kNEHotspotHelperCommandTypeFilterScanList) {

            for (network  in cmd.networkList) {

                NSLog(@"COMMAND TYPE After:   %ld", (long)cmd.commandType);

                if ([network.SSID isEqualToString:@"A8F8"]) {

                    double signalStrength = network.signalStrength;

                    NSLog(@"Signal Strength: %f", signalStrength);

                    [network setConfidence:kNEHotspotHelperConfidenceHigh];

                    [network setPassword:@"123456789"];

                    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);