一: 介绍

近几年,智能设备越来越火,这些智能设备中,有很大一部分是通过手机来控制硬件设备,来达到预期的效果,这中间少不了要使用到蓝牙功能,通过蓝牙来通信来控制设备。
除了蓝牙控制设备之外,还可以通过Wi-Fi来控制设备,iOS11前只能跳转到系统设置界面手动连接Wi-Fi,iOS11之后苹果提供 NEHotspotConfiguration ,NEHotspotConfigurationManager 类直连周边Wi-Fi。

这篇文章主要和大家分享iOS11之后在App内自动连接Wi-Fi,Wi-Fi信息获取,Wi-Fi检测等功能。

二:权限配置

苹果提供的 NEHotspotConfiguration ,NEHotspotConfigurationManager需要在开发者账号和项目中做如下配置。

1. 打开App IDs Hotspot 权限

登陆https://developer.apple.com,如果App ID已经存在,只需增加Hotspot 权限,如果App ID不存在,新建一个并添加Hotspot 权限。

配置的App ID需要与项目中的Bundle ID一致。

2. 添加依赖库NetworkExtension.framework

在项目中Build Phases - Link Binary With Libraries中添加 依赖库NetworkExtension.framework

3. 打开Capabilities里的Hotspot Configuration

Xcode - Capabilities - Hostpot Configuration 开关打开

三:NEHotspotConfiguration库分析

在NEHotspotConfiguration库中有3个属性,分别是:

  1. SSID:要连的wifi名称
  2. joinOnce:默认是NO,会保留配置过的wifi,YES即是不保存
  3. lifeTimeInDays: 配置的生命周期

源码如下:

/*!* @property SSID* @discussion SSID of the Wi-Fi Network.*/
@property (readonly) NSString * SSID API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @property joinOnce* @discussion if set to YES the configuration will not be persisted. Default is NO.*/
@property BOOL joinOnce API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @property lifeTimeInDays* @discussion The lifetime of the configuration in days. The configuration is stored for the*   number of days specified by this property. The minimum value is 1 day and maximum value is 365 days.*   A configuration does not get deleted automatically if this property is not set or set to an invalid value.*   This property does not apply to Enterprise and HS2.0 networks.*/
@property (copy) NSNumber * lifeTimeInDays API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);

有4个实例化方法,分别是:

  1. 无密码的开放网络
  2. 受保护的WEP或WPA / WPA2个人Wi-Fi网络创建由SSID标识的新热点配置
  3. 具有EAP设置的WPA / WPA2企业Wi-Fi网络
  4. 具有HS 2.0和EAP设置的Hotspot 2.0 Wi-Fi网络

源码如下:

/*!* @method initWithSSID:* @discussion*   A designated initializer to instantiate a new NEHotspotConfiguration object.*   This initializer is used to configure open Wi-Fi Networks.** @param SSID The SSID of the Open Wi-Fi Network.*   Length of SSID must be between 1 and 32 characters.*/
- (instancetype)initWithSSID:(NSString *)SSID API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @method initWithSSID:passphrase:isWEP* @discussion*   A designated initializer to instantiate a new NEHotspotConfiguration object.*   This initializer is used configure either WEP or WPA/WPA2 Personal Wi-Fi Networks.** @param SSID The SSID of the WEP or WPA/WPA2 Personal Wi-Fi Network* @param passphrase The passphrase credential.*   For WPA/WPA2 Personal networks: between 8 and 63 characters.*   For Static WEP(64bit)  : 10 Hex Digits*   For Static WEP(128bit) : 26 Hex Digits* @param isWEP YES specifies WEP Wi-Fi Network else WPA/WPA2 Personal Wi-Fi Network*/
- (instancetype)initWithSSID:(NSString *)SSIDpassphrase:(NSString *)passphrase isWEP:(BOOL)isWEP API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @method initWithSSID:eapSettings* @discussion*   A designated initializer to instantiate a new NEHotspotConfiguration object.*   This initializer is used configure WPA/WPA2 Enterprise Wi-Fi Networks.** @param SSID The SSID of WPA/WPA2 Enterprise Wi-Fi Network* @param eapSettings EAP configuration*/
- (instancetype)initWithSSID:(NSString *)SSIDeapSettings:(NEHotspotEAPSettings *)eapSettings API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @method initWithHS20Settings:eapSettings* @discussion*   A designated initializer to instantiate a new NEHotspotConfiguration object.*   This initializer is used configure HS2.0 Wi-Fi Networks.** @param hs20Settings Hotspot 2.0 configuration* @param eapSettings EAP configuration*/
- (instancetype)initWithHS20Settings:(NEHotspotHS20Settings *)hs20SettingseapSettings:(NEHotspotEAPSettings *)eapSettings API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);

四:NEHotspotConfigurationManager库分析

在NEHotspotConfigurationManager库中提供了四个方法,分别是:

  1. 应用你的Configuration,会弹出系统框询问是否加入
  2. 通过ssid删除一个配置
  3. 删除Hotspot 2.0域名标识的Wi-Fi热点配置
  4. 获取配置过的wifi名称。如果你设置joinOnce为YES,这里就不会有了

源码如下:

/*!* @method applyConfiguration:* @discussion This function adds or updates a Wi-Fi network configuration.* @param configuration NEHotspotConfiguration object containing the Wi-Fi network configuration.* @param completionHandler A block that will be called when add/update operation is completed.*   This could be nil if application does not intend to receive the result.*   The NSError passed to this block will be nil if the configuration is successfully stored, non-nil otherwise.*   If the configuration is found invalid or API encounters some other error then completionHandler is called*   with instance of NSError containing appropriate error code. This API attempts to join the Wi-Fi network*   if the configuration is successfully added or updated and the network is found nearby.**/
- (void)applyConfiguration:(NEHotspotConfiguration *)configurationcompletionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @method removeConfigurationForSSID:* @discussion This function removes Wi-Fi configuration.*   If the joinOnce property was set to YES, invoking this method will disassociate from the Wi-Fi network*   after the configuration is removed.* @param SSID Wi-Fi SSID for which the configuration is to be deleted.*/
- (void)removeConfigurationForSSID:(NSString *)SSID API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @method removeConfigurationForNetworkName:* @discussion This function removes Wi-Fi configuration.* @param domainName HS2.0 domainName for which the configuration is to be deleted.*/
- (void)removeConfigurationForHS20DomainName:(NSString *)domainName API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);/*!* @method getConfiguredSSIDsWithCompletionHandler:* @discussion This function returns array of SSIDs and HS2.0 Domain Names that the calling application has configured.*   It returns nil if there are no networks configurred by the calling application.*/
- (void)getConfiguredSSIDsWithCompletionHandler:(void (^)(NSArray<NSString *> *))completionHandler API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);

到这里配置及源码分析的前期工作已经做完。

五:加入Wi-Fi实现解析

首先引入 NetworkExtension 库

#import <NetworkExtension/NetworkExtension.h>

然后需要调用NEHotspotConfiguration库方法,根据不同的情况,选择使用不同的方法,这里使用受保护的WEP或WPA举例

NEHotspotConfiguration *hotspotConfig = [[NEHotspotConfiguration alloc]initWithSSID:_wifiName.text passphrase:_wifiPassword.text isWEP:NO];

然后开始连接 ,调用applyConfiguration此方法后系统会自动弹窗确认,根据返回的error.code来判断Wi-Fi是否加入成功,error code = 7 为用户点击了弹框取消按钮,error code = 13 为已连接

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:hotspotConfig completionHandler:^(NSError * _Nullable error) {if (error && error.code != 13 && error.code != 7) {NSLog(@"加入失败");}else if(error.code ==7){NSLog(@"已取消");}else{NSLog(@"已连接");}}];

五:Wi-Fi列表实现解析

在Android中设备周围的Wi-Fi信息是可以扫面获取到的,iOS设备至今也没有完全开放相关接口,如果真有该需求
,需要填写申请表申请,通过后方可使用,咱们这里分享的Wi-Fi列表,是NEHotspotConfigurationManager库中的getConfiguredSSIDsWithCompletionHandler方法,可以获取到已经保存过的Wi-Fi信息,实现源码如下:

[[NEHotspotConfigurationManager sharedManager] getConfiguredSSIDsWithCompletionHandler:^(NSArray<NSString *> * array) {for (NSString * str in array) {self.WiFiListTextView.text = [NSString stringWithFormat:@"%@\n%@",self.WiFiListTextView.text,str];NSLog(@"加入过的WiFi:%@",str);}}];

六:Wi-Fi测速实现解析

通过Wi-Fi测速可以获取到Wi-Fi强度、上行速度、下行速度,我在demo中封装了SpeedController类来实现该功能,通过下面两个方法来实现:

//获取信号强度(0到1)
+(void)getSignalStrength:(void(^)(float signalStrength))resultBlock;
//获取下行速度,上行速度(单位是 MB/S)
-(void)getDownstreamSpeedAndUpstreamSpeed:(void(^)(float downstreamSpeed,float upstreamSpeed))resultBlock;

整理出 45 本电子版书籍

源码Demo获取方法

关注 【网罗开发】微信公众号,回复【93】便可领取。
网罗天下方法,方便你我开发,更多iOS技术干货等待领取,所有文档会持续更新,欢迎关注一起成长!

iOS App无需跳转系统设置自动连接Wi-Fi相关推荐

  1. iOS应用内跳转系统设置相关界面的方法

    在iOS开发中,有时会有跳转系统设置界面的需求,例如提示用户打开蓝牙或者WIFI,提醒用户打开推送或者位置权限等.在iOS6之后,第三方应用需要跳转系统设置界面,需要在URL type中添加一个pre ...

  2. iOS 11上跳转系统设置

    经常收到朋友和一些群友问这个问题,其实网上有很多类似文章,大多数都有些问题,下面简单记录一下.     在iOS 10上刚出现不能跳转的时候,就找过很多资料去实现跳转,最后好像是在stackoverf ...

  3. ios快捷指令使用指南-TUST校园网认证自动连接登录

    ios快捷指令使用指南-TUST校园网认证自动连接登录 2021.9.1更新: 校园网登录页面更新后,部分规则变动,更新后快捷指令链接如下: https://www.icloud.com/shortc ...

  4. iOS开发之如何跳到系统设置里的各种设置界面

    一·iOS开发之如何跳到系统设置里的WiFi界面 之前以为,苹果不支持直接从应用跳到系统设置里的WiFi界面.后来发现,这个小功能是可以实现的,而且实现起来并不麻烦.让我们一起来看看吧! 需求 从应用 ...

  5. IOS app蓝牙连接硬件设备 自动断开问题

    最近自己也在开发蓝牙问题 遇到手机连接上  硬件设置被之后过不了多久就自动断开连接 .而且是定时的 .断开的连接提示Timeout intervail  并且安卓的手机连接设备 却不会断开. 找了下 ...

  6. iOS 10 跳转系统设置

    苦心人天不负, 为了项目终于把 iOS 10 跳转系统设置的方法给搞定了, 很欣慰. http://www.cnblogs.com/lurenq/p/6189580.html iOS 10 跳转系统设 ...

  7. win10系统设置开机网络自动连接的方法

    当我们要连接电脑网络的时候,都是需要输入宽带和密码才能进行登录,很是麻烦,那么在win10系统中有没有什么方法可以设置开机网络自动连接呢,这样就方便多了,下面给大家分享一下win10系统设置开机网络自 ...

  8. 微信分享链接打开无法下载APP的解决方案(微信自动跳转浏览器打开下载链接)

    需求分析 目前的APP基本都支持二维码扫描下载,二维码下载也成为了大家用起来很顺手的一种方式.由于微信的用户基本占据了国内市场的 90%,说到扫一扫用户第一个想到的就是打开微信扫一下,通过微信分享AP ...

  9. iOS跳转系统设置方法支持iOS7~iOS10+

    iOS跳转系统设置方法支持iOS7~iOS10+ _array = @[@{@"系统设置":@"App-Prefs:root=INTERNET_TETHERING&quo ...

最新文章

  1. 基于自动驾驶车辆的NVIDIA-TensorRT推理实时优化
  2. 在React Hook里使用history.push跳转
  3. ASP.NET开源框架之HIPPO技术内幕(四)--多语言的实现
  4. CNN几种经典模型比较
  5. python开发环境的搭建和设置
  6. Android点击图片随机,android 设置图片随机出现-两种方式
  7. c语言标准化考试程序报告,在线考试系统-c语言报告.doc
  8. 利用BioPerl将DNA序列翻译成蛋白序列
  9. 从JDK源码角度看Short
  10. 故事的小黄花,从CSDN那年就飘着
  11. 1.2执行更高级的线程任务(Performing More Advanced Thread Tasks)
  12. 聊聊我所从事过的通信行业
  13. CS 61A Spring 2019 HW01 学习笔记
  14. Java中get和set的作用、用法
  15. W806串口管脚复用
  16. 适当修改LIO-SAM_based_relocalization解决初始重定位显示错误
  17. 全网域名已注册、未注册批量查询工具
  18. 怎样用excel剔除异常数据_如何剔除异常数据?
  19. 关于Python将plt文件坐标转换为TXT文本
  20. 注册时要求获取手机短信码的实现(java)

热门文章

  1. 光储充一体化充电站_光储充一体化充电站有哪些好处
  2. [fuzz论文阅读] Symbolic execution for software testing: three decades later
  3. wifi+vcam项目丢帧机制调整
  4. 结构主义(structuralism)
  5. 3年收10亿,普陀山悄悄改名重启IPO
  6. 除了两个胖子还有easyman,天弘指数基金是漫画营销圈的MVP!
  7. 你见过的最差的程序员是怎样的?
  8. 算法练习——判断链表是否有环 leetcode.141 python
  9. 谷歌浏览器怎么长截图怎么截_Chrome谷歌浏览器如何截取整个网页长图?
  10. cocos2d-x下硬件音量按键控制游戏音量的实现