Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

swift - CNCopyCurrentNetworkInfo with iOS 13

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...