【发布时间】:2012-11-02 12:58:56
【问题描述】:
是否可以运行返回用户连接的无线网络名称的方法?在我的应用程序内部,我希望能够返回用户连接到的无线网络的名称。
【问题讨论】:
是否可以运行返回用户连接的无线网络名称的方法?在我的应用程序内部,我希望能够返回用户连接到的无线网络的名称。
【问题讨论】:
这对我来说很完美:
#import <SystemConfiguration/CaptiveNetwork.h>
CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
// NSLog(@"SSID: %@",CFDictionaryGetValue(myDict, kCNNetworkInfoKeySSID));
NSString *networkName = CFDictionaryGetValue(myDict, kCNNetworkInfoKeySSID);
if ([networkName isEqualToString:@"Hot Dog"])
{
self.storeNameController = [[StoreDataController alloc] init];
[self.storeNameController addStoreNamesObject];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Connection Failed"
message: @"Please connect to the Hot Dog network and try again"
delegate: self
cancelButtonTitle: @"Close"
otherButtonTitles: nil];
[alert show];
【讨论】:
从 Developer.apple ,您可以使用 CNCopyCurrentNetworkInfo
返回给定网络接口的当前网络信息。
CFDictionaryRef CNCopyCurrentNetworkInfo (
CFStringRef interfaceName
);
它包含一个包含接口当前网络信息的字典。所有权遵循创建规则。
注意:适用于 iOS 4.1 及更高版本。
示例:
这个例子在真机上可以正常运行,在模拟器中可能会崩溃。
添加SystemConfiguration.framework
导入 CaptiveNetwork header 同下
#import <SystemConfiguration/CaptiveNetwork.h>
然后编写以下代码。
CFArrayRef myArray = CNCopySupportedInterfaces();
// Get the dictionary containing the captive network infomation
CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
NSLog(@"Information of the network we're connected to: %@", captiveNtwrkDict);
NSDictionary *dict = (__bridge NSDictionary*) captiveNtwrkDict;
NSString* ssid = [dict objectForKey:@"SSID"];
NSLog(@"network name: %@",ssid);
或
使用 Bonjour,应用程序既在本地网络上宣传自己,又在网络上显示该应用程序的其他实例的列表
查看示例Witap 应用程序
【讨论】:
CNCopyCurrentNetworkInfo 听起来很有希望,但我无法让它正常运行。 Wifi的接口名称是什么?我搜索了它,但没有找到。
坏消息:'CNCopyCurrentNetworkInfo' 已弃用:在 iOS 9.0 中首次弃用 - 对于强制网络应用程序,它已完全被
好消息:虽然已被弃用,但它在 iOS 9 中仍然有效,并且在 iOS 10 中已被弃用。
它在 iOS 13 中仍然有效,但前提是应用实现了定位服务并且用户授权了它们的使用。
【讨论】: