OC网络学习17:连接WiFi热点

张建 lol

网络模式

我们知道手机网络模式有两种:一种是无线 WiFi模式,另一种是蜂窝数据 Cellular(2/3/4/5G)

前言

近几年,智能设备越来越火,这些智能设备中,有很大一部分是 通过手机来控制硬件设备,来达到预期的效果,这中间少不了要使用到 蓝牙功能通过蓝牙来通信来控制设备

除了蓝牙控制设备之外,还可以通过 Wi-Fi来控制设备,iOS11前只能跳转到系统设置界面手动连接Wi-Fi,iOS11之后苹果提供 NEHotspotConfigurationNEHotspotConfigurationManager 类直连周边Wi-Fi。

例如:你买了一个网络摄像头,你可以连上摄像头的Wi-Fi热点去配置这个摄像头

介绍

本文主要介绍如何使用 NEHotSpotConfiguration 类,连接WiFi热点,NEHotSpotConfiguration 能够很方便地通过在手机App上的操作来实现热点的链接。

权限配置

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

  • 打开App IDs Hotspot 权限

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

  • 打开Capabilities里的Hotspot Configuration

Xcode -> Target -> Capabilities -> Hostpot Configuration 添加

NEHotspotConfiguration 库分析

  1. NEHotspotConfiguration 库中有3个属性,分别是:
  • SSID:要连的wifi名称
  • joinOnce:默认是NO,会保留配置过的wifi,YES即是不保存
  • lifeTimeInDays: 配置的生命周期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*!
* @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个实例化方法,分别是:

  • 无密码的开放网络

  • 受保护的WEP或WPA / WPA2个人Wi-Fi网络创建由SSID标识的新热点配置

  • 具有EAP设置的WPA / WPA2企业Wi-Fi网络

  • 具有HS 2.0和EAP设置的Hotspot 2.0 Wi-Fi网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*!
* @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 *)SSID
passphrase:(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 *)SSID
eapSettings:(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 *)hs20Settings
eapSettings:(NEHotspotEAPSettings *)eapSettings API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);

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

  • 应用你的 Configuration,会弹出系统框询问是否加入

  • 通过ssid删除一个配置

  • 删除Hotspot 2.0域名标识的Wi-Fi热点配置

  • 获取配置过的wifi名称。如果你设置 joinOnce 为 YES,这里就不会有了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*!
* @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 *)configuration
completionHandler:(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);

使用

  • 首先引入 NetworkExtension 库
1
#import <NetworkExtension/NetworkExtension.h>
  • 然后需要调用 NEHotspotConfiguration 库方法,根据不同的情况,选择使用不同的方法,这里使用受保护的WEP或WPA举例
1
NEHotspotConfiguration *hotspotConfig = [[NEHotspotConfiguration alloc]initWithSSID:@"WiFi名" passphrase:@"WiFi密码" isWEP:NO];
  • 然后开始连接 ,调用 applyConfiguration 此方法后系统会自动弹窗确认,根据返回的error.code来判断Wi-Fi是否加入成功
1
2
3
4
[[NEHotspotConfigurationManager sharedManager] applyConfiguration:hotspotConfig completionHandler:^(NSError * _Nullable error) {
NSLog(@"error code:%d",error.code);
}

  • 获取到已经保存过的Wi-Fi信息
1
2
3
4
5
6
[[NEHotspotConfigurationManager sharedManager] getConfiguredSSIDsWithCompletionHandler:^(NSArray<NSString *> * array) {
for (NSString * str in array) {
NSLog(@"加入过的WiFi:%@",str);
}
}];

  • Post title:OC网络学习17:连接WiFi热点
  • Post author:张建
  • Create time:2023-07-17 15:10:30
  • Post link:https://redefine.ohevan.com/2023/07/17/OC网络/OC网络学习17:连接WiFi热点/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
On this page
OC网络学习17:连接WiFi热点