An overview of the

With the release of iOS 9, apple introduced NetworkExtension, a framework that allows you to do a lot of web-related things. This article mainly introduces how to use NEHotspotHelper to get the device WiFi list.

Demo address: github.com/EyreFree/EF…

I. Matters needing attention

  1. First of all, NEHotspotHelper is only supported on iOS 9 and above, not previous versions of iOS.
  2. Then, you need a developer account;
  3. Finally, there is no large-scale open using the framework, so you need to apply to the apple sent and approved was able to obtain permission to use this framework, content is roughly describe the reason why you need to use the framework, and then I is to use English to describe (thanks to baidu and Google translation), but it is said that in Chinese. After the filing of probably will receive feedback email within a week, apply for address is: developer.apple.com/contact/net… .

2. Create App ID

Open the apple developer center, log in and find the App IDs options, click the button to create an App ID used to create next Provisioning Profile, address is: developer.apple.com/account/ios… , as shown in the figure:

First, fill in Name and Bundle ID, which are both EFNEHotspotHelperDemo, as shown in the figure below:

Next, check the Option of Wireless Accessory Configuration, as shown in the figure:

Then observe that the status as shown in the figure indicates that it has been opened successfully:

Check the newly created App ID in the list of App IDs:

3. Create a Provisioning Profile

Find the Provisioning Profiles option, click the button to create a Provisioning Profile used to create a sample project, then the address is: developer.apple.com/account/ios… , as shown in the figure:

Firstly, select the Profile type. Here, I choose iOS App Development. You can freely choose according to your specific needs:

Next, select the App ID we created in the second step, as shown in the figure:

Then select certificate and device, select all:

In the step of extra permission, we need to select the Network Extension permission we have applied for. It can be seen that it contains the NEHotspotHelper permission we need to use, as shown in the figure:

After filling in the Profile Name, we can successfully create the Profile we need:

Click Download to Download it locally:

Double-click to open to add a Profile to this machine:

You can view the Profile installed in XCode’s account Settings. If the Profile is not installed successfully, you can try to Download it again by clicking the Download button in Action:

4. Create project

Let’s create a sample project to show how to get the WiFi list. First, change the Bundle ID to EFNEHotspotHelperDemo as previously set:

Then add the background mode permission array to info.plist:

The code is as follows:

<key>UIBackgroundModes</key>
<array>
  <string>network-authentication</string>
</array>
Copy the code

You can see that background mode is enabled in Target -> Capabilities after you add it:

Next find the Wireless Accessory Configuration in Capabilities and open it up:

Found in the project, called name} {engineering. Entitlements file, for example in the Demo should be EFNEHotspotHelperDemo. Entitlements, add HotspotHelper access code:

The code is as follows:

<key>com.apple.developer.networking.HotspotHelper</key>
<true/>
Copy the code

Now that you’ve done all the messy configuration work, you can try building. If there are no error messages, then you can happily use HotspotHelper; If there is a problem, check that the previous steps have been completed correctly or modify specific items based on the error message.

Five. Core code

First, add a header reference where HotspotHelper is needed, using objective-C code as an example:

#import <NetworkExtension/NetworkExtension.h>
Copy the code

Then use the following code to print the WiFi list information to the XCode console. Note: Here, you need to open the WIRELESS LAN page in system Settings to obtain the relevant information, because the callback will be triggered when the system refreshes WiFi information on this page:

- (void)scanWifiInfos{
    NSLog(@"1.Start");

    NSMutableDictionary* options = [[NSMutableDictionary alloc] init];
    [options setObject:@"EFNEHotspotHelperDemo" forKey: kNEHotspotHelperOptionDisplayName];
    dispatch_queue_t queue = dispatch_queue_create("EFNEHotspotHelperDemo", NULL);

    NSLog(@"2.Try");
    BOOL returnType = [NEHotspotHelper registerWithOptions: options queue: queue handler: ^(NEHotspotHelperCommand * cmd) {

        NSLog(@"4.Finish");
        NEHotspotNetwork* network;
        if(cmd.commandType == kNEHotspotHelperCommandTypeEvaluate || cmd.commandType == kNEHotspotHelperCommandTypeFilterScanList) {// Iterate through the WiFi list to print basic informationfor (network in cmd.networkList) {
                NSString* wifiInfoString = [[NSString alloc] initWithFormat: @"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ nSSID: % @ \ nMac address: % @ \ n signal strength: % f \ nCommandType: % ld \ n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n \ n", network.SSID, network.BSSID, network.signalStrength, (long)cmd.commandType];
                NSLog(@"% @", wifiInfoString); // If the specified WiFi is detected, the password can be set for direct connectionif ([network.SSID isEqualToString: @"Test WiFi"]) {
                    [network setConfidence: kNEHotspotHelperConfidenceHigh];
                    [network setPassword: @"123456789"];
                    NEHotspotHelperResponse *response = [cmd createResponse: kNEHotspotHelperResultSuccess];
                    NSLog(@"Response CMD: %@", response);
                    [response setNetworkList: @[network]];
                    [response setNetwork: network]; [response deliver]; }}}}]; // Registration succeededreturnType returns a Yes value, otherwise No NSLog(@)"3.Result: %@".returnType == YES ? @"Yes" : @"No");
}
Copy the code

6. Demonstrate

Well, the Demo runs as follows. Click Open WiFi Setting button to directly Open the WLAN page:

For details, download Demo and complete the configuration: github.com/EyreFree/EF…

7. Note

Thanks for completing the Demo by referring to the following materials:

IOS 9.0 search for nearby wi-fi hotspots iOS NetworkExtension framework using notes iOS NEHotspotHelper use ios-network extension-nehotspothelper API Reference – NetworkExtension


This paper links: LPD – ios. Making. IO / 2017/03/09 /…

Please correct any intellectual property rights, copyright issues or theoretical errors.

Please indicate the original author and above information.