NEHotspotConfigurationManager使用

作者: cj小牛 | 来源:发表于2018-02-23 14:39 被阅读465次

NEHotspotConfigurationManager 加入已经密码的wifi网络多用于物联网。设备配置加入互联网。

1.在开发者创建Bunder Identifier 在 35295D1A-69EF-4AC5-886F-E0842F2E46D6.png
如图勾选Hotpost

2.创建工程在Capabilities 中勾选Hotspot configuration


7CBBF69E-5C62-47E7-957E-158A716FDF3C.png (如图)
3.在Build Phases 中 4B49C6A7-5D14-4FB5-8660-CE750E43025C.png
4.在info.plist 中加入权限
以上三部环境配置完成了

下面开始使用
1.在使用的地方加入头文件 #import <NetworkExtension/NEHotspotConfigurationManager.h>

  1. NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc]initWithSSID:@"wifi名" passphrase:@"wifi密码" isWEP:NO]; //加入有密码的wifi

// NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc]initWithSSID:@"wifi名"]; //加入没有密码的wifi

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
    if([[self currentIphoneConnectedWifiName]isEqualToString:@"NET"]) {

            NSLog(@"加入网络成功");


    }
    NSLog(@"%@",error);
}];

这个方法存在一个问题,如果你加入一个不存在的WiFi,会弹出无法加入WiFi的弹框,但是本方法的回调error没有值。在这里,我是通过判断当前wifi是否是我要加入的wifi来解决这个问题的。

后面是辅助的方法 查看手机当前连接的wifi 先要导入头文件

import <SystemConfiguration/CaptiveNetwork.h>

/** 获取当前手机连接到到Wi-Fi 的名字 */

  • (NSString *)currentIphoneConnectedWifiName{

    NSString *wifiName = nil;

    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    if (!wifiInterfaces)return nil;

NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;

for (NSString *interfaceName in interfaces){

    CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));

    if (dictRef){

        NSDictionary *networkInfoDic = (__bridge NSDictionary *)dictRef;

        wifiName = [networkInfoDic objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
        CFRelease(dictRef);
    }
}
CFRelease(wifiInterfaces);

return wifiName;

}

相关文章

网友评论

    本文标题:NEHotspotConfigurationManager使用

    本文链接:https://www.haomeiwen.com/subject/rolxxftx.html