1

I'm currently trying to display the SSID of a user's connected WiFi and compare it to a particular SSID, for example, the set SSID is 'WirelessHotspot'.

When the user's connected WiFi is 'WirelessHotspot', the app will display that it is connected to the correct WiFi and also display the WiFi name.

Currently, I have tried this code, referenced from Get SSID in Swift 2:

import UIKit

import Foundation
import SystemConfiguration.CaptiveNetwork

public class SSID {
    class func fetchSSIDInfo() ->  String {
        var currentSSID = ""
        if 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

                }
            }
            self.networkname.text = String(currentSSID)
        }
        return currentSSID
    }
}

class AttendanceScreen: UIViewController {

    @IBOutlet weak var networkname: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

However, this code:

self.networkname.text = String(currentSSID)

Will return error:

Type 'SSID' has no member 'networkname'

So, how can I implement this in Swift for iOS 9? Thanks in advance!

Community
  • 1
  • 1
Panda
  • 6,955
  • 6
  • 40
  • 55
  • You might want to take a look here: http://codereview.stackexchange.com/questions/78318/getting-wifi-ssid-on-ios-in-swift – luk2302 Jan 22 '16 at 15:13
  • Please [search](http://stackoverflow.com/search?q=%5Bswift%5D+ssid) before posting. – rmaddy Jan 22 '16 at 15:16
  • How do I display the SSID in a label, I can't display the SSID, the codes also shows how to get the SSID. Thanks for your quick response! – Panda Jan 22 '16 at 15:17
  • Update your question with relevant code giving you trouble. Setting the `text` property of a `UILabel` is pretty trivial. – rmaddy Jan 22 '16 at 15:35

1 Answers1

1

I figured out that it would be much easier to create a bridge from Swift to Objective-C.

Importing framework:

#import <SystemConfiguration/CaptiveNetwork.h>

Code to get the SSID of user's connected WiFi:

func getMAC()->(success:Bool,ssid:String,mac:String){

    if let cfa: NSArray = CNCopySupportedInterfaces() {
        for x in cfa {
            if let dict = CFBridgingRetain(CNCopyCurrentNetworkInfo(x as! CFString)) {
                let ssid = dict ["SSID"]!
                let mac  = dict["BSSID"]!
                return (true, ssid as! String, mac as! String)
            }
        }
    }
    return (false,"","")
}

Print and display in a label when needed:

let x = getMAC()
    if x.success {
        MAClabel = x.mac
        SSIDlabel = x.ssid
        print(x.mac)
        print (x.ssid)
    }

I hope that those with this question would find this useful!

Community
  • 1
  • 1
Panda
  • 6,955
  • 6
  • 40
  • 55