iOS11 App内自动连接Wi-Fi

背景:给智能设备配置网络,需要直连智能设备发射的Wi-Fi

目前技术:iOS11后苹果提供  NEHotspotConfigurationManager 类直连周边Wi-Fi,iOS11前只能跳转到系统设置界面手动连接Wi-Fi

步骤

1.给开发者中心给 Appid 配置连接Wi-Fi的权限

iOS11 App内自动连接Wi-Fi

2.Xcode – Build Phases – 引入NetworkExtension

iOS11 App内自动连接Wi-Fi

3.Xcode – Capabilities – Hostpot Configuration 勾选

iOS11 App内自动连接Wi-Fi

代码实现

引入 #import

if (@available(iOS 11.0, *)) {
        NEHotspotConfiguration * hotspotConfig = [[NEHotspotConfiguration alloc] initWithSSID:@"Deli_L1050ADNW_1B0000"];
       // 开始连接 (调用此方法后系统会自动弹窗确认)
        [[NEHotspotConfigurationManager sharedManager] applyConfiguration:hotspotConfig completionHandler:^(NSError * _Nullable error) {
            NSLog(@"%@",error);
            if (error && error.code != 13 && error.code != 7) {

            }else if(error.code ==7){//error code = 7 :用户点击了弹框取消按钮


            }else{// error code = 13 :已连接


            }

        }];
    } else {
// iOS11以下版本逻辑
    }

 

以上说的方法不需要去苹果申请权限

注意事项

由于NEHotspotConfigurationManager.h在模拟器上不可用,导入方法为:

#if TARGET_IPHONE_SIMULATOR
#else
#import 
#endif

代码逻辑同于注意事项1

作者:小岂几哥