CNCopyCurrentNetworkInfo with iOS 13

只谈情不闲聊 提交于 2019-11-27 23:28:19

问题


Apple changed some things regarding WiFi with iOS 13. If you want to use CNCopyCurrentNetworkInfo your app needs to have one of the following

  • Apps with permission to access location
  • Your app is the currently enabled VPN app
  • Your app configured the WiFi network the device is currently using via NEHotspotConfiguration

Source: WWDC 19 session 713

I am configuring a network using NEHotspotConfiguration but I can not get the current SSID anymore after doing so.

The following code worked fine with iOS 12:

/// retrieve the current SSID from a connected Wifi network  
private func retrieveCurrentSSID() -> String? {  
    let interfaces = CNCopySupportedInterfaces() as? [String]  
    let interface = interfaces?  
        .compactMap { [weak self] in self?.retrieveInterfaceInfo(from: $0) }  
        .first  

    return interface  
}  

/// Retrieve information about a specific network interface  
private func retrieveInterfaceInfo(from interface: String) -> String? {  
    guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as CFString) as? [String: AnyObject],  
        let ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String  
        else {  
            return nil  
    }  
    return ssid  
} 

With iOS 13 CNCopyCurrentNetworkInfo always returns nil.

My app has the Access WiFi Information Capability set.

Thanks for your help!


回答1:


As I said on the Apple Developer Forums use of CNCopyCurrentNetworkInfo is now limited.

Check out WWDC 19 session 713, Advances in Networking, Part 2 (maybe 75% of the way through the presentation). CNCopyCurrentNetworkInfo is now only available to your app in three cases:

  • Apps with permission to access location
  • Your app is the currently enabled VPN app
  • Your app configured the WiFi network the device is currently using via NEHotspotConfiguration

If you don't meet at least one of these conditions CNCopyCurrentNetworkInfo will always return nil in iOS 13.

UPDATE: As of iOS 13 Developer Beta 6, Apple has finally updated the documentation to note the changes.




回答2:


I have a similar issue in my app. I have submitted a feedback form to Apple and got a response stating:

Potential fix identified - For a future OS update

So hopefully, this will be resolved before the final release (not in iOS 13 Beta 4 though).

For a workaround, you can set joinOnce = false in your NEHotspotConfiguration. In my app, it allowed me to access CNCopySupportedInterfaces, but required me to remove configuration every time my app was closed.

Hope it helps!

Edit:

It seems that in iOS 13 beta 5 issue no longer persists. In my application, I can access CNCopyCurrentNetworkInfo again (thus confirming the Wi-Fi has been connected), no matter if NEHotspotConfiguration.joinOnce flag is set to true or false.




回答3:


Starting with iOS 13, the CNCopyCurrentNetworkInfo API will no longer return valid Wi-Fi SSID and BSSID information. Instead, the information returned by default will be:

SSID: “Wi-Fi” or “WLAN” (“WLAN" will be returned for the China SKU) BSSID: "00:00:00:00:00:00"

If your app is using this API, Apple now encourage you to adopt alternative approaches that don’t require Wi-Fi or network information. Valid SSID and BSSID information from CNCopyCurrentNetworkInfo will still be provided to VPN apps, apps that have used NEHotspotConfiguration to configure the current Wi-Fi network, and apps that have obtained permission to access user location through Location Services.

You can Test your app on the latest iOS 13 beta to make sure it works properly. If your app requires valid Wi-Fi SSID and BSSID information to function, you can do the following: For accessory setup apps, use the NEHotSpotConfiguration API, which now has the option to pass a prefix of the SSID hotspot your app expects to connect to. For other types of apps, use the CoreLocation API to request the user’s consent to access location information.




回答4:


If someone tries to use CNCopyCurrentNetworkInfo via the case Apps with permission to access location (via the CoreLocation API and CLLocationManager) don't forget to enable the Access WiFi Information capability (See also this answer here). This should be mandatory since iOS 12, but I needed to update an App which was last tested before iOS 12. See also from the Discussion Section here:

Important

To use this function, an app linked against iOS 12 or later must enable the Access WiFi Information capability in Xcode. For more information, see Access WiFi Information Entitlement. Calling this function without the entitlement always returns NULL when linked against iOS 12 or later.




回答5:


Must verify that

<key>com.apple.developer.networking.wifi-info</key>
<true/>

is added to Entitlements-Release.plist, mostly its just added to Entitlements-Debug.plist only




回答6:


For me, TryCopyCurrentNetworkInfo always return nil even with a network configured with NEHotspotConfiguration. I tried the the recommendation of Mateusz with setting the option JoinOne to false or true but it did not solve the issue.

Phone OS is 13.1.3 Access WiFi information is enabled in my profile It was working in iOS 12




回答7:


I've spent hours trying to figure out a way to let CNCopyCurrentNetworkInfo works on iOS 13 with no results, no matter location permissions status or the fact that my app configured the current Wi-Fi network the device is currently using via NEHotspotConfiguration. It just does not work.

I finally came out with the reliable solution to get the SSID through getConfiguredSSIDsWithCompletionHandler: method of NEHotspotConfigurationManager.

Here's a simple example:

static func retrieveCurrentSSID(  callback: @escaping ( String? ) -> Void ){
    NEHotspotConfigurationManager.shared.getConfiguredSSIDs { ( networkSSIDs ) in
        callback( networkSSIDs.first )
    }
}




回答8:


I believe that you match one of the following condition which is suggested by Apple:

1 :Apps with permission to access location

2 :Your app is the currently enabled VPN app

3 :Your app configured the WiFi network the device is currently using via NEHotspotConfiguration

In my case even i have location services enable when was facing the issue.

After some workaround here are i found the two solution which you can try:

1: Reboot the device. This works for me!!.

2: Update the OS if available.

I am not sure it helps you but in few cases it should work, few people suggest reboot 2-3 times work for them.

Another good news from Apple that they fix for this issue on iOS 13.3 beta version



来源:https://stackoverflow.com/questions/56583650/cncopycurrentnetworkinfo-with-ios-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!