

iOS11 App内自动连接Wi-Fi
source link: http://www.cocoachina.com/ios/20181018/25235.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

背景:给智能设备配置网络,需要直连智能设备发射的Wi-Fi
目前技术:iOS11后苹果提供 NEHotspotConfigurationManager 类直连周边Wi-Fi,iOS11前只能跳转到系统设置界面手动连接Wi-Fi
步骤
1.给开发者中心给 Appid 配置连接Wi-Fi的权限
2.Xcode - Build Phases - 引入NetworkExtension
3.Xcode - Capabilities - Hostpot Configuration 勾选
代码实现
引入 #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
作者:小岂几哥
链接:https://www.jianshu.com/p/376262eb9079
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK