一、申请NetworkExtension框架,NetworkExtension框架申请链接
二、配置工程
1.新建iCloud Containers以及App IDs配置(添加iCloud 和Wireless Accessory服务)
2.plist文件配置以及Capabilities配置
之后向.entitlements文件里添加一个BOOL值为YES的字段 com.apple.developer.networking.HotspotHelper
三、实现代码
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:@"可用wifi" forKey:kNEHotspotHelperOptionDisplayName];
dispatch_queue_t queue = dispatch_queue_create("com.myapp.ex", NULL);
BOOL returnType = [NEHotspotHelper registerWithOptions:options queue:queue handler: ^(NEHotspotHelperCommand * cmd) {
//系统定时搜索附近的wifi以及切换wifi时会触发该block
NEHotspotNetwork *network;
[cmd createResponse:kNEHotspotHelperResultAuthenticationRequired];
if (cmd.commandType == kNEHotspotHelperCommandTypeEvaluate || cmd.commandType ==kNEHotspotHelperCommandTypeFilterScanList) {
for (network in cmd.networkList) {
NSLog(@"COMMAND TYPE After:%ld", (long)cmd.commandType);
//network.secure\network.autoJoined\network.justJoined
//对指定ssid做标记
if ([network.SSID isEqualToString:@"ssid"]|| [network.SSID isEqualToString:@"zjdljt"]) {
double signalStrength = network.signalStrength;
[network setConfidence:kNEHotspotHelperConfidenceHigh];
//设置wifi的密码,当连接该wifi时不用输入密码就可以连接wifi(从后台请求附近wifi以及密码)
[network setPassword:@"0086985336"];
NEHotspotHelperResponse *response = [cmd createResponse:kNEHotspotHelperResultSuccess];
//当有多个分享wifi时,可以把wifi依次加入到数组中,并把数组作为setNetworkList的参数
[response setNetworkList:@[network]];
[response setNetwork:network];
[response deliver];
}
}
}
}];