简体   繁体   English

不推荐使用 CaptiveNetwork 并且已阻止对 Wifi 名称的调用后,如何在 iOS9 中获取 Wifi SSID

[英]How to get Wifi SSID in iOS9 after CaptiveNetwork is deprecated and calls for Wifi name are already blocked

Until today I used the CaptiveNetwork Interface to display the name of the currently connected Wifi.直到今天,我使用 CaptiveNetwork Interface 来显示当前连接的 Wifi 的名称。 The iOS 9 Prerelease reference already stated, that the CaptiveNetwork methods are depracted now, but they still worked at the beginning. iOS 9 预发布参考已经声明,CaptiveNetwork 方法现在已弃用,但它们在开始时仍然有效。

With the newest version Apple seems to have blocked this calls already (maybe due to privacy concerns?).使用最新版本,Apple 似乎已经阻止了此调用(可能是出于隐私问题?)。

Is there any other way to get the name of the current Wifi?有没有其他方法可以获取当前 Wifi 的名称?

This is how I obtained the SSID until today, but you only get nil now:这就是我直到今天获得 SSID 的方式,但你现在只得到零:

#import <SystemConfiguration/CaptiveNetwork.h>

NSString *wifiName = nil;  
NSArray *interFaceNames = (__bridge_transfer id)CNCopySupportedInterfaces(); 

for (NSString *name in interFaceNames) { 
    NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name); 

    if (info[@"SSID"]) { 
        wifiName = info[@"SSID"]; 
    } 
} 

Register your app as Hotspot helper.将您的应用注册为热点助手。

#import <NetworkExtension/NetworkExtension.h>  

NSArray * networkInterfaces = [NEHotspotHelper supportedNetworkInterfaces];  
NSLog(@"Networks %@",networkInterfaces);  

UPDATE (Sept. 11th, 2015)更新(2015 年 9 月 11 日)

The following Captive Network APIs have been re-enabled in the latest version of iOS 9 instead.以下 Captive Network API 已在最新版本的 iOS 9 中重新启用。

  • CNCopySupportedInterfaces CNCopy 支持的接口
  • CNCopyCurrentNetworkInfo CNCCopyCurrentNetworkInfo

UPDATE (Sept. 16th, 2015)更新(2015 年 9 月 16 日)

If you still prefer to use NetworkExtension and Apple gave you permission to add the entitlements, then you can do this to get the wifi information:如果您仍然更喜欢使用NetworkExtension并且 Apple 允许您添加权限,那么您可以这样做以获取 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;
}

NetworkExtension provides you some extra information as secure, auto joined or the signal strength. NetworkExtension为您提供一些额外的信息,如安全、自动加入或信号强度。 And it also allows you to set credential to wifis on background mode, when the user scans wifis around.当用户扫描周围的 wifi 时,它还允许您在后台模式下为 wifi 设置凭据。

In the GM for iOS 9, it seems like this is enabled again.在 iOS 9 的 GM 中,似乎再次启用了此功能。 In fact, it's not even listed as deprecated in the online documentation, however the CaptiveNetwork header file does have the following:事实上,它甚至没有在在线文档中列为已弃用,但是 CaptiveNetwork 头文件确实具有以下内容:

CNCopySupportedInterfaces (void) __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_8, __MAC_NA, __IPHONE_4_1, __IPHONE_9_0, CN_DEPRECATION_NOTICE);

So, it is working in the iOS 9 GM, but not sure for how long :)所以,它在 iOS 9 GM 中工作,但不确定多长时间:)

The answer by abdullahselek is still correct even for Swift 4.1 and 4.2.即使对于 Swift 4.1 和 4.2,abdullahselek 的答案仍然是正确的。

A small caveat is that now in iOS 12, you must go to the capabilities section of your app project and enable the Access WiFi Information feature.一个小小的警告是,现在在 iOS 12 中,您必须转到应用项目的功能部分并启用访问 WiFi 信息功能。 It will add an entitlement entry to your project and allow the function call CNCopyCurrentNetworkInfo to work properly.它将向您的项目添加一个权利条目,并允许函数调用CNCopyCurrentNetworkInfo正常工作。

If you do not do this, that function simply returns nil.如果您不这样做,该函数只会返回 nil。 No errors or warnings at runtime about the missing entitlement will be displayed.运行时不会显示有关缺少授权的错误或警告。

For more info, see the link below to Apple's documentation.有关更多信息,请参阅下面指向 Apple 文档的链接。

https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo

Confirm on 2017-April-27, Captive Network is still working for Swift 3.1 , XCode 8.3 2017 年 4 月 27 日确认,Captive Network 仍在为Swift 3.1XCode 8.3

For Swift > 3.0对于Swift > 3.0

func printCurrentWifiInfo() {
  if let interface = CNCopySupportedInterfaces() {
    for i in 0..<CFArrayGetCount(interface) {
      let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interface, i)
      let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
      if let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString), let interfaceData = unsafeInterfaceData as? [String : AnyObject] {
        // connected wifi
        print("BSSID: \(interfaceData["BSSID"]), SSID: \(interfaceData["SSID"]), SSIDDATA: \(interfaceData["SSIDDATA"])")
      } else {
        // not connected wifi
      }
    }
  }
}

For Objective-C对于Objective-C

NSArray *interFaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();

for (NSString *name in interFaceNames) {
  NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name);

  NSLog(@"wifi info: bssid: %@, ssid:%@, ssidData: %@", info[@"BSSID"], info[@"SSID"], info[@"SSIDDATA"]);
 }

As mentioned before CaptiveNetwork works well with Xcode 8.3 and upper.如前所述,CaptiveNetwork 适用于 Xcode 8.3 及更高版本。 Here are code snippets for both Swift 3 , Swift 4 and Objective-C .以下是Swift 3Swift 4Objective-C 的代码片段。

Swift 3 & 4斯威夫特 3 & 4

import SystemConfiguration.CaptiveNetwork

internal class SSID {

    class func fetchSSIDInfo() -> [String: Any] {
        var interface = [String: Any]()
        if let interfaces = CNCopySupportedInterfaces() {
            for i in 0..<CFArrayGetCount(interfaces){
                let interfaceName = CFArrayGetValueAtIndex(interfaces, i)
                let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
                guard let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString) else {
                    return interface
                }
                guard let interfaceData = unsafeInterfaceData as? [String: Any] else {
                    return interface
                }
                interface = interfaceData
            }
        }
        return interface
    }

}

Objective-C目标-C

#import <SystemConfiguration/CaptiveNetwork.h>

+ (NSDictionary *)fetchSSIDInfo
{
    NSArray *interFaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();
    for (NSString *name in interFaceNames)
    {
        NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name);
        return info;
    }
    return nil;
}

This should be working now with iOS 13.3.这现在应该适用于 iOS 13.3。 I'm using a related Pod library that uses the exact function in Objc and with a Swift wrapper.我正在使用一个相关的 Pod 库,它使用 Objc 中的确切函数和 Swift 包装器。

https://github.com/Feghal/FGRoute https://github.com/Feghal/FGRoute

CaptiveNetwork is still working. CaptiveNetwork 仍在工作。 Due to many requests Apple may have reinstated the API's.由于许多请求,Apple 可能已经恢复了 API。

Using CaptiveNetwork we can get the SSID of the WiFi network.使用 CaptiveNetwork 我们可以获得 WiFi 网络的 SSID。 It even works in iOS 10.它甚至适用于 iOS 10。

#import <SystemConfiguration/CaptiveNetwork.h>

NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name);

Here is the output:这是输出:

Printing description of info:
{
    BSSID = "5*:**:**:**:**:**";
    SSID = Cisco12814;
    SSIDDATA = <43697363 6f313238 3134>;
}

CaptiveNetwork is still working. CaptiveNetwork 仍在工作。 But you will need to add this:但是您需要添加以下内容:

com.apple.developer.networking.wifi-info = true inside your Entitlements.plist. com.apple.developer.networking.wifi-info = true 在您的 Entitlements.plist 中。

Plus, you need to be Enable the Access WiFi Information in the App ID part in your developer.apple.com portal.另外,您需要在 developer.apple.com 门户的 App ID 部分启用访问 WiFi 信息。

Be sure, to clean your environment, generate new provisioning profile after enabling option "Access WiFi Information" in the App ID.请务必在启用 App ID 中的“访问 WiFi 信息”选项后生成新的配置文件以清理您的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM