iOS-Developer-OCiOS锦囊

iOS 获取系统wifi列表,wifi信号强度,并给wifi设置

2018-08-30  本文已影响71人  路漫漫其修远兮Wzt

转载自:iOS 获取系统wifi列表,wifi信号强度,并给wifi设置密码,标签(副标题)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ws1352864983/article/details/73481436

最近公司社区项目中要用到或者社区周围WiFi列表以及信号强度,好多网友说实现不了讨论区


相关资源:

APP跳转 及iOS10跳转在设置页面

Wifi 定位原理及 iOS Wifi 列表获取

iOS 获取系统wifi列表,wifi信号强度,并给wifi设置密码,标签(副标题)

iOS 无法获取 WiFi 列表?一定是因为你不知道这个框架

Demo地址


9.0以前获取需要引入

#import<SystemConfiguration/CaptiveNetwork.h>

+ (NSString *)getWifiName{

NSString *wifiName = nil;

CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();

if (!wifiInterfaces) {

return @"未知";

}

NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;

for (NSString *interfaceName in interfaces) {

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

if (dictRef) {

NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;

wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];

CFRelease(dictRef);

}

}

CFRelease(wifiInterfaces);

return wifiName;

}

9.0以后获取需要引入

import <NetworkExtension/NetworkExtension.h>

但在应用中需要添加icloud containers

NSArray * networkInterfaces = [NEHotspotHelper supportedNetworkInterfaces];

NSLog(@"Networks %@",networkInterfaces);

//获取wifi列表

for(NEHotspotNetwork *hotspotNetwork in [NEHotspotHelper supportedNetworkInterfaces]) {

NSString *ssid = hotspotNetwork.SSID;

NSString *bssid = hotspotNetwork.BSSID;

BOOL secure = hotspotNetwork.secure;

BOOL autoJoined = hotspotNetwork.autoJoined;

double signalStrength = hotspotNetwork.signalStrength;}

应用程序的Info.plist必须添加一个包含“remote-notification”的UIBackgroundModes数组.

应用程序必须设置“com.apple.developer.networking.HotspotHelper’作为它的权利之一。

该权利的值是一个布尔值true要申请这个权利,请发送E-MAIL到networkextension@apple.com更多信息请参阅苹果的Hotspot Network Subsystem Programming Guide

申请链接 :https://developer.apple.com/contact/network-extension/

详阅官方文档: https://developer.apple.com/reference/networkextension/nehotspothelper

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/Hotspot_Network_Subsystem_Guide/Contents/Introduction.html#//apple_ref/doc/uid/TP40016639

Register a Hotspot Helper

+ (BOOL)registerWithOptions:(NSDictionary*)options queue:(dispatch_queue_t)queue handler:(NEHotspotHelperHandler)handler

@param options

kNEHotspotHelperOptionDisplayName :WIFI的注释tag字符串// 此处设置的内容会在WiFi列表中每个WiFi下边展示出来

@param queue dispatch_queue_t 用来调用handle的block

@param handler NEHotspotHelperHandler block 用于执行处理 helper commands.

@return 注册成功YES, 否则NO.

@discussion 一旦这个API调用成功,应用程序有资格在后台启动,并参与各种热点相关的功能。 当应用程序启动此方法应该调用一次。再次调用它会不会产生影响,并返回NO。

Manage Hotspot Networks

+ (BOOL)logoff:(NEHotspotNetwork *)network

@param network 对应当前关联的WiFi网络NEHotspotNetwork

@return 注销命令已成功进入队列YES, 否则NO.

@discussion 调用此方法使kNEHotspotHelperCommandTypeLogoff型的NEHotspotHelperCommand向应用程序发出的“handler”模块 网络参数必须符合当前关联的WiFi网络,即它必须来自对NEHotspotHelperCommand网络属性或方法supportedInterfaces

+ (NSArray *)supportedNetworkInterfaces

@return 如果没有网络接口被管理,返回nil。否则,返回NEHotspotNetwork对象数组。

@discussion 每个网络接口由NEHotspotNetwork对象表示。当前返回的数组包含一个NEHotspotNetwork对象代表Wi-Fi接口。

这种方法的主要目的是当没有得到一个命令来处理它时,让一个热点助手偶尔提供在UI里其准确的状态。 此方法加上NEHotspotNetwork的isChosenHelper方法允许应用程序知道它是否是当前处理的网络。

NSMutableDictionary* options = [[NSMutableDictionary alloc] init];

[options setObject:@"我是副标题" forKey:kNEHotspotHelperOptionDisplayName];

dispatch_queue_t queue = dispatch_queue_create("com.myapp.ex", NULL);

BOOL returnType = [NEHotspotHelper registerWithOptions:options queue:queue handler: ^(NEHotspotHelperCommand * cmd) {

NEHotspotNetwork* network;

NSLog(@"COMMAND TYPE:   %ld", (long)cmd.commandType);

[cmd createResponse:kNEHotspotHelperResultAuthenticationRequired];

if (cmd.commandType == kNEHotspotHelperCommandTypeEvaluate || cmd.commandType ==kNEHotspotHelperCommandTypeFilterScanList) {

NSLog(@"WIFILIST:   %@", cmd.networkList);

for (network  in cmd.networkList) {

NSLog(@"COMMAND TYPE After:   %ld", (long)cmd.commandType);

if ([network.SSID isEqualToString:@"ssid"]|| [network.SSID isEqualToString:@"WISPr Hotspot"]) {

double signalStrength = network.signalStrength;

NSLog(@"Signal Strength: %f", signalStrength);

[network setConfidence:kNEHotspotHelperConfidenceHigh];

[network setPassword:@"password"];

NEHotspotHelperResponse *response = [cmd createResponse:kNEHotspotHelperResultSuccess];

NSLog(@"Response CMD %@", response);

[response setNetworkList:@[network]];

[response setNetwork:network];

[response deliver];

}

}

}

}];

NSLog(@"result :%d", returnType);

NSArray *array = [NEHotspotHelper supportedNetworkInterfaces];

NSLog(@"wifiArray:%@", array);

NEHotspotNetwork *connectedNetwork = [array lastObject];

NSLog(@"supported Network Interface: %@", connectedNetwork);
上一篇下一篇

猜你喜欢

热点阅读