NEHotspotHelper尝试在iOS上验证网络问题

时间:2015-09-16 09:53:41

标签: ios objective-c

我已使用Network Extension API注册了我的应用,并在评论here后启用了后台模式。因此,当用户进入wifi设置时,我能够看到范围内的wifis列表,但我不知道为什么我无法在所需的wifis上设置我的自定义句子并设置密码。 / p>

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

SSID是匹配的,所以代码在所需的wifi上执行,Try here应该出现在MySSID下的wifi设置上,密码也应该应用,但两者都不是工作

对代码中的错误有什么想法吗?感谢

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。在找到ssid时,我没有创建响应,而是创建一个数组并添加与我的ssid匹配的网络,然后调用createResponse:设置我的数组中的所有网络:

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];
    }
}];

现在我可以看到"试试这里"在SSID名称下,我可以加入网络,无需手动添加密码。