Pablo A.
Pablo A.

Reputation: 2052

NEHotspotHelper issue trying to authenticate network on iOS

I have registered my app for using the Network Extension API and enabled the background mode following the comments here. So, I am able to see the list of wifis on range when the user goes to the wifi settings, but I don't know why I am not able to set my custom sentence on the desired wifis and set the password.

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:@"Try here", kNEHotspotHelperOptionDisplayName, nil];

dispatch_queue_t queue = dispatch_queue_create("com.myapp.ex", 0);  // dispatch_get_main_queue()

BOOL isAvailable = [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:@"MySSID"]) {

                [network setConfidence:kNEHotspotHelperConfidenceHigh];
                [network setPassword:@"mypassword"];

                // This is required
                NEHotspotHelperResponse *response = [cmd createResponse:kNEHotspotHelperResultSuccess];
                [response setNetwork:network];
                [response deliver];

                NSLog(@"Confidence set to high for ssid: %@ (%@)\n\n", network.SSID, network.BSSID);
            }  
        }    
    }
}];

The SSID is matched, so the code is executed over the desired wifi and Try here should appear under MySSID on the wifi settings, and the password should be applied too, but none of both things are working.

Any idea about what's wrong on the code? Thanks

Upvotes: 2

Views: 2486

Answers (1)

Pablo A.
Pablo A.

Reputation: 2052

I found a solution. Instead of creating a response when the ssid is found what I've done is to create an array and add the networks matching my ssid and then call createResponse: setting all the networks in my array:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:@"Try here", kNEHotspotHelperOptionDisplayName, nil];

dispatch_queue_t queue = dispatch_queue_create("com.myapp.ex", 0);

BOOL isAvailable = [NEHotspotHelper registerWithOptions:options queue:queue handler: ^(NEHotspotHelperCommand * cmd) {

    NSMutableArray *hotspotList = [NSMutableArray new];

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

        for (NEHotspotNetwork* network  in cmd.networkList) {

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

                [network setConfidence:kNEHotspotHelperConfidenceHigh];
                [network setPassword:@"mypassword"];

                NSLog(@"Confidence set to high for ssid: %@ (%@)\n\n", network.SSID, network.BSSID);

                [hotspotList addObject:network];
            }  
        }

        NEHotspotHelperResponse *response = [cmd createResponse:kNEHotspotHelperResultSuccess];
        [response setNetworkList:hotspotList];
        [response deliver];
    }
}];

Now I can see "Try here" under the SSID name and I can join the network with no need for manually adding the password.

Upvotes: 2

Related Questions