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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31555640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:57:07  来源:igfitidea点击:

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

iosobjective-ccocoa-touchios9

提问by Thyraz

Until today I used the CaptiveNetwork Interface to display the name of the currently connected Wifi. The iOS 9 Prerelease reference already stated, that the CaptiveNetwork methods are depracted now, but they still worked at the beginning.

直到今天,我使用 CaptiveNetwork Interface 来显示当前连接的 Wifi 的名称。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"]; 
    } 
} 

采纳答案by lewiguez

In the GM for iOS 9, it seems like this is enabled again. In fact, it's not even listed as deprecated in the online documentation, however the CaptiveNetwork header file does have the following:

在 iOS 9 的 GM 中,似乎再次启用了此功能。事实上,它甚至没有在在线文档中列为已弃用,但是 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 中工作,但不确定多长时间:)

回答by Pablo A.

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
  • CNCopyCurrentNetworkInfo
  • CNCopy 支持的接口
  • CNCCopyCurrentNetworkInfo

UPDATE (Sept. 16th, 2015)

更新(2015 年 9 月 16 日)

If you still prefer to use NetworkExtensionand 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;
}

NetworkExtensionprovides you some extra information as secure, auto joined or the signal strength. And it also allows you to set credential to wifis on background mode, when the user scans wifis around.

NetworkExtension为您提供一些额外的信息,如安全、自动加入或信号强度。当用户扫描周围的 wifi 时,它还允许您在后台模式下为 wifi 设置凭据。

回答by FerrousGuy

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 Informationfeature. It will add an entitlement entry to your project and allow the function call CNCopyCurrentNetworkInfoto work properly.

一个小小的警告是,现在在 iOS 12 中,您必须转到应用项目的功能部分并启用访问 WiFi 信息功能。它将为您的项目添加一个权利条目,并允许函数调用CNCopyCurrentNetworkInfo正常工作。

If you do not do this, that function simply returns nil. No errors or warnings at runtime about the missing entitlement will be displayed.

如果您不这样做,该函数只会返回 nil。运行时不会显示有关缺少授权的错误或警告。

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

回答by nuynait

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"]);
 }

回答by abdullahselek

As mentioned before CaptiveNetwork works well with Xcode 8.3 and upper. Here are code snippets for both Swift 3, Swift 4and Objective-C.

如前所述,CaptiveNetwork 适用于 Xcode 8.3 及更高版本。以下是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;
}

回答by T? Minh Ti?n

CaptiveNetwork is still working. But you will need to add this:

CaptiveNetwork 仍在工作。但是您需要添加以下内容:

com.apple.developer.networking.wifi-info = true inside your Entitlements.plist.

Plus, you need to be Enable the Access WiFi Information in the App ID part in your developer.apple.com portal.

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

另外,您需要在 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 信息”选项后生成新的配置文件以清理您的环境。

回答by Nick N

This should be working now with iOS 13.3. I'm using a related Pod library that uses the exact function in Objc and with a Swift wrapper.

这现在应该适用于 iOS 13.3。我正在使用一个相关的 Pod 库,它使用 Objc 中的确切函数和 Swift 包装器。

https://github.com/Feghal/FGRoute

https://github.com/Feghal/FGRoute

回答by Ranjith

CaptiveNetwork is still working. Due to many requests Apple may have reinstated the API's.

CaptiveNetwork 仍在工作。由于许多请求,Apple 可能已经恢复了 API。

Using CaptiveNetwork we can get the SSID of the WiFi network. It even works in iOS 10.

使用 CaptiveNetwork 我们可以获得 WiFi 网络的 SSID。它甚至适用于 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>;
}