iPhone get SSID without private library

后端 未结 9 923
[愿得一人]
[愿得一人] 2020-11-22 06:00

I have a commercial app that has a completely legitimate reason to see the SSID of the network it is connected to: If it is connected to a Adhoc network for a 3rd party har

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 06:51

    Here's the short & sweet Swift version.

    Remember to link and import the Framework:

    import UIKit
    import SystemConfiguration.CaptiveNetwork
    

    Define the method:

    func fetchSSIDInfo() -> CFDictionary? {
        if let
            ifs = CNCopySupportedInterfaces().takeUnretainedValue() as? [String],
            ifName = ifs.first,
            info = CNCopyCurrentNetworkInfo((ifName as CFStringRef))
        {
            return info.takeUnretainedValue()
        }
        return nil
    }
    

    Call the method when you need it:

    if let
        ssidInfo = fetchSSIDInfo() as? [String:AnyObject],
        ssID = ssidInfo["SSID"] as? String
    {
        println("SSID: \(ssID)")
    } else {
        println("SSID not found")
    }
    

    As mentioned elsewhere, this only works on your iDevice. When not on WiFi, the method will return nil – hence the optional.

提交回复
热议问题