Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift CNCopySupportedInterfaces not valid

Tags:

ios

swift

Trying to get the SSID of current device. I have found plenty of examples on how to do it however I am struggling with getting the CNCopySupportedInterfaces to autocomplete. I have 'import SystemConfiguration' at the top of my swift file but no success. Can't seem to figure out what I am doing wrong.

like image 611
shreddish Avatar asked Jul 31 '15 21:07

shreddish


4 Answers

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

You need: import SystemConfiguration.CaptiveNetwork

Underneath the covers, CaptiveNetwork is a C header file (.h) that is within the SystemConfiguration framework:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SystemConfiguration.framework/Headers/CaptiveNetwork.h

If you know Objective-C, this goes into more depth:

iPhone get SSID without private library

You have to use the awkward syntax to bridge from any pure C API, so the following is required:

for interface in CNCopySupportedInterfaces().takeRetainedValue() as! [String] {
    println("Looking up SSID info for \(interface)") // en0
    let SSIDDict = CNCopyCurrentNetworkInfo(interface).takeRetainedValue() as! [String : AnyObject]
    for d in SSIDDict.keys {
        println("\(d): \(SSIDDict[d]!)")
    }
}

ADDENDUM FOR SWIFT 2.2 and 3.0

The CFxxx datatypes are now bridged to native Objective-C runtime, eliminating the head-scratching retain calls. However, nullable pointers give rise to Optionals, so things don't get any shorter. At least, it's fairly clear what's going on, plus the nil helps us identify the simulator. The other answer uses an awful lot of bit-casting and unsafe operations which seems non-Swiftian, so I offer this.

func getInterfaces() -> Bool {
    guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else {
        print("this must be a simulator, no interfaces found")
            return false
        }
        guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else {
        print("System error: did not come back as array of Strings")
        return false
    }
    for interface in swiftInterfaces {
        print("Looking up SSID info for \(interface)") // en0
        guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface) else {
            print("System error: \(interface) has no information")
            return false
        }
        guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else {
        print("System error: interface information is not a string-keyed dictionary")
            return false
        }
        for d in SSIDDict.keys {
            print("\(d): \(SSIDDict[d]!)")
        }
    }
    return true
}

Output on success:

SSIDDATA: <57696c6d 79>

BSSID: 12:34:56:78:9a:bc

SSID: YourSSIDHere

like image 185
BaseZen Avatar answered Sep 19 '22 16:09

BaseZen


In Swift 2.0 / iOS 9 the API CaptiveNetwork is (nearly) gone or depreciated. I contacted Apple regarding this problem and I thought we could (or should) use the NEHotspotHelper instead. I got a respond from Apple today: One should continue to use CaptiveNetwork and the two relevant APIs (even tough there marked depreciated):

 CNCopySupportedInterfaces
 CNCopyCurrentNetworkInfo

The user braime posted an updated code-snippet for this problem on Ray Wenderlich forums:

 let interfaces:CFArray! = CNCopySupportedInterfaces()
    for i in 0..<CFArrayGetCount(interfaces){
        let interfaceName: UnsafePointer<Void> 
          =  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
        } else {
            currentSSID = ""
        }
    }

Works perfect for me.

like image 35
hibento Avatar answered Sep 21 '22 16:09

hibento


Swift:

import SystemConfiguration.CaptiveNetwork

func currentSSIDs() -> [String] {
        guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
            return []
        }
        return interfaceNames.flatMap { name in
            guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String:AnyObject] else {
                return nil
            }
            guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
                return nil
            }
            return ssid
        }
    }

Then print(currentSSIDs()), not working on simulator, only real devices.

Taken from https://forums.developer.apple.com/thread/50302

like image 27
Juan Boero Avatar answered Sep 23 '22 16:09

Juan Boero


func getInterfaces() -> String?  {
    var ssid: String?
    if let interfaces = CNCopySupportedInterfaces() as NSArray? {
        for interface in interfaces {
            if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
                break
            }
        }
    }
    return ssid
}

In iOS 12 and up you will need to enable the Access WiFi Information capability for your app in order to get the ssid

like image 42
ironRoei Avatar answered Sep 20 '22 16:09

ironRoei