Get SSID in Swift 2

前端 未结 10 1363
情书的邮戳
情书的邮戳 2020-12-05 05:36

Im trying to use this code to get SSID

import Foundation
import SystemConfiguration.CaptiveNetwork

public class SSID {
    class func getSSID() -> String         


        
10条回答
  •  死守一世寂寞
    2020-12-05 06:20

    This may help you (tested on Swift 2):

    import Foundation
    import SystemConfiguration.CaptiveNetwork
    
    public class SSID {
        class func fetchSSIDInfo() -> String {
            var currentSSID = ""
            if let interfaces = CNCopySupportedInterfaces() {
                for i in 0.. = CFArrayGetValueAtIndex(interfaces, i)
                    let rec = unsafeBitCast(interfaceName, AnyObject.self)
                    let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
                    if unsafeInterfaceData != nil {
                        let interfaceData = unsafeInterfaceData! as Dictionary!
                        currentSSID = interfaceData["SSID"] as! String
                    }
                }
            }
            return currentSSID
        }
    }
    

    I took and adapted the code from Ray Wenderlich's site (once was here: Retrieve SSID in iOS9 but now the specific thread has been removed from site)

    iOS 12

    You must enable Access WiFi Information from capabilities.

    Important To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App ID. Documentation link

    Swift4.2

    public class SSID {
        class func fetchSSIDInfo() -> String {
            var currentSSID = ""
            if let interfaces = CNCopySupportedInterfaces() {
                for i in 0..

提交回复
热议问题