iOS11实现App内自动连接Wi-Fi的方法

时间:2022-11-01 17:29:45

背景:

给智能设备配置网络,需要直连智能设备发射的wi-fi

目前技术:

ios11后苹果提供  nehotspotconfigurationmanager 类直连周边wi-fi,ios11前只能跳转到系统设置界面手动连接wi-fi

步骤

给开发者中心给 appid 配置连接wi-fi的权限

iOS11实现App内自动连接Wi-Fi的方法

xcode - build phases - 引入networkextension

iOS11实现App内自动连接Wi-Fi的方法

xcode - capabilities - hostpot configuration 勾选

iOS11实现App内自动连接Wi-Fi的方法

代码实现

引入 #import <networkextension/nehotspotconfigurationmanager.h>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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在模拟器上不可用,导入方法为:

?
1
2
3
4
#if target_iphone_simulator
#else
#import <networkextension/nehotspotconfigurationmanager.h>
#endif

代码逻辑同于注意事项1

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/376262eb9079