我有一个商业应用程序,它有完全合法的理由来查看其连接到的网络的SSID:如果将其连接到用于第三方硬件设备的Adhoc网络,则它的工作方式必须与运行时不同。连接到互联网。
我所看到的有关获取SSID的所有信息都告诉我,我必须使用Apple80211,据我了解,这是一个私有库。我还读到,如果我使用私有库,Apple不会批准该应用程序。
我是被困在一个苹果和一个困难的地方之间,还是在这里我缺少什么?
我有一个商业应用程序,它有完全合法的理由来查看其连接到的网络的SSID:如果将其连接到用于第三方硬件设备的Adhoc网络,则它的工作方式必须与运行时不同。连接到互联网。
我所看到的有关获取SSID的所有信息都告诉我,我必须使用Apple80211,据我了解,这是一个私有库。我还读到,如果我使用私有库,Apple不会批准该应用程序。
我是被困在一个苹果和一个困难的地方之间,还是在这里我缺少什么?
Answers:
从iOS 7或8开始,您可以执行此操作(需要iOS 12+的权利,如下所示):
@import SystemConfiguration.CaptiveNetwork;
/** Returns first non-empty SSID network info dictionary.
* @see CNCopyCurrentNetworkInfo */
- (NSDictionary *)fetchSSIDInfo {
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);
NSDictionary *SSIDInfo;
for (NSString *interfaceName in interfaceNames) {
SSIDInfo = CFBridgingRelease(
CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
NSLog(@"%s: %@ => %@", __func__, interfaceName, SSIDInfo);
BOOL isNotEmpty = (SSIDInfo.count > 0);
if (isNotEmpty) {
break;
}
}
return SSIDInfo;
}
输出示例:
2011-03-04 15:32:00.669 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: Supported interfaces: (
en0
)
2011-03-04 15:32:00.693 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: en0 => {
BSSID = "ca:fe:ca:fe:ca:fe";
SSID = XXXX;
SSIDDATA = <01234567 01234567 01234567>;
}
请注意,模拟器上不支持ifs。在您的设备上测试。
您必须从功能启用访问wifi信息。
重要提示要在iOS 12及更高版本中使用此功能,请在Xcode中为您的应用启用访问WiFi信息功能。启用此功能后,Xcode会自动将Access WiFi Information授权添加到您的授权文件和App ID中。文档链接
斯威夫特4.2
func getConnectedWifiInfo() -> [AnyHashable: Any]? {
if let ifs = CFBridgingRetain( CNCopySupportedInterfaces()) as? [String],
let ifName = ifs.first as CFString?,
let info = CFBridgingRetain( CNCopyCurrentNetworkInfo((ifName))) as? [AnyHashable: Any] {
return info
}
return nil
}
@import
而不是#import
),更新了ARC友好代码。当导入模块(而不只是标题)时,Clang将自动链接所需的框架。
CNCopyCurrentNetworkInfo
返回有用信息的功能之外,iOS 13 beta现在还需要位置许可。否则返回nil
。请参阅:stackoverflow.com/questions/56583650/...
这是基于@elsurudo的代码的清理后的ARC版本:
- (id)fetchSSIDInfo {
NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
NSLog(@"Supported interfaces: %@", ifs);
NSDictionary *info;
for (NSString *ifnam in ifs) {
info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
NSLog(@"%@ => %@", ifnam, info);
if (info && [info count]) { break; }
}
return info;
}
在iOS 10中不再弃用CNCopySupportedInterfaces。(API参考)
您需要导入SystemConfiguration / CaptiveNetwork.h并将SystemConfiguration.framework添加到目标的链接库(在构建阶段)。
这是swift的代码片段(RikiRiocma的答案):
import Foundation
import SystemConfiguration.CaptiveNetwork
public class SSID {
class func fetchSSIDInfo() -> String {
var currentSSID = ""
if let interfaces = CNCopySupportedInterfaces() {
for i in 0..<CFArrayGetCount(interfaces) {
let interfaceName: UnsafePointer<Void> = CFArrayGetValueAtIndex(interfaces, i)
let rec = unsafeBitCast(interfaceName, AnyObject.self)
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
if unsafeInterfaceData != nil {
let interfaceData = unsafeInterfaceData! as Dictionary!
currentSSID = interfaceData["SSID"] as! String
}
}
}
return currentSSID
}
}
(重要: CNCopySupportedInterfaces在模拟器上返回nil。)
对于Objective-c,请参见此处和下方的Esad答案
+ (NSString *)GetCurrentWifiHotSpotName {
NSString *wifiName = nil;
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *ifnam in ifs) {
NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info[@"SSID"]) {
wifiName = info[@"SSID"];
}
}
return wifiName;
}
从iOS 9开始,不提倡使用Captive Network *。(来源)
*不再在iOS 10中弃用,请参见上文。
建议您使用NEHotspotHelper(源代码)
您将需要向apple发送电子邮件至networkextension@apple.com并请求权利。(来源)
示例代码(不是我的代码。请参见Pablo A的答案):
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;
}
旁注:是的,他们在iOS 9中弃用了CNCopySupportedInterfaces,并改变了它们在iOS 10中的位置。我与一位苹果网络工程师进行了交谈,在有很多人提出Radars并在Apple Developer论坛上谈论此问题之后,情况发生了逆转。
这对我在设备(而非模拟器)上有效。确保添加系统配置框架。
#import <SystemConfiguration/CaptiveNetwork.h>
+ (NSString *)currentWifiSSID {
// Does not work on the simulator.
NSString *ssid = nil;
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *ifnam in ifs) {
NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info[@"SSID"]) {
ssid = info[@"SSID"];
}
}
return ssid;
}
此代码可以很好地获得SSID。
#import <SystemConfiguration/CaptiveNetwork.h>
@implementation IODAppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
NSLog(@"Connected at:%@",myDict);
NSDictionary *myDictionary = (__bridge_transfer NSDictionary*)myDict;
NSString * BSSID = [myDictionary objectForKey:@"BSSID"];
NSLog(@"bssid is %@",BSSID);
// Override point for customization after application launch.
return YES;
}
这是结果:
Connected at:{
BSSID = 0;
SSID = "Eqra'aOrange";
SSIDDATA = <45717261 27614f72 616e6765>;
}
如果您运行的是iOS 12,则需要执行额外的步骤。我一直在努力使此代码起作用,并最终在Apple网站上找到了它:“重要的是要在iOS 12及更高版本中使用此功能,请在Xcode中为您的应用启用Access WiFi Information功能。启用此功能时,Xcode会自动将访问WiFi信息权利添加到您的权利文件和应用ID中。” https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo
请参阅CaptiveNetwork中的CNCopyCurrentNetworkInfo:http : //developer.apple.com/library/ios/#documentation/SystemConfiguration/Reference/CaptiveNetworkRef/Reference/reference.html。
这是简短而甜美的Swift版本。
记住要链接并导入框架:
import UIKit
import SystemConfiguration.CaptiveNetwork
定义方法:
func fetchSSIDInfo() -> CFDictionary? {
if let
ifs = CNCopySupportedInterfaces().takeUnretainedValue() as? [String],
ifName = ifs.first,
info = CNCopyCurrentNetworkInfo((ifName as CFStringRef))
{
return info.takeUnretainedValue()
}
return nil
}
在需要时调用该方法:
if let
ssidInfo = fetchSSIDInfo() as? [String:AnyObject],
ssID = ssidInfo["SSID"] as? String
{
println("SSID: \(ssID)")
} else {
println("SSID not found")
}
如其他地方所述,这仅适用于您的iDevice。不使用WiFi时,该方法将返回nil-因此是可选的。
从iOS 13开始,除非您配置了当前网络或具有VPN配置,否则您的应用还需要访问核心位置才能使用该CNCopyCurrentNetworkInfo
功能:
因此,这是你所需要的(见苹果文档):
-链接CoreLocation.framework
库
-添加location-services
为UIRequiredDeviceCapabilities
一个键/值Info.plist中
-添加NSLocationWhenInUseUsageDescription
一个键/值Info.plist中描述为什么您的应用程序需要核心定位
-添加“访问无线网络您的应用的“信息”权利
现在作为一个Objective-C示例,首先使用以下命令检查是否接受位置访问,然后再读取网络信息CNCopyCurrentNetworkInfo
:
- (void)fetchSSIDInfo {
NSString *ssid = NSLocalizedString(@"not_found", nil);
if (@available(iOS 13.0, *)) {
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(@"User has explicitly denied authorization for this application, or location services are disabled in Settings.");
} else {
CLLocationManager* cllocation = [[CLLocationManager alloc] init];
if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
[cllocation requestWhenInUseAuthorization];
usleep(500);
return [self fetchSSIDInfo];
}
}
}
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge_transfer id)CNCopyCurrentNetworkInfo(
(__bridge CFStringRef)ifnam);
NSDictionary *infoDict = (NSDictionary *)info;
for (NSString *key in infoDict.allKeys) {
if ([key isEqualToString:@"SSID"]) {
ssid = [infoDict objectForKey:key];
}
}
}
...
...
}