10 Replies
      Latest reply: Nov 12, 2017 6:31 PM by claoudj RSS
      ampuma Level 1 Level 1 (0 points)

        Hello,

         

        How do I find my SSID wifi swift 3?

         

        Thank you for your help....

         

        Bruno.

        • Re: Find my current SSID in Swift 3 ?
          hrobnik Level 1 Level 1 (0 points)

          hey, you can do it with this code, it doesnt work in Simulator!!

           

          func fetchSSIDInfo() ->  String {
                  var currentSSID = ""
                  if let interfaces:CFArray = CNCopySupportedInterfaces() {
                      for i in 0..<CFArrayGetCount(interfaces){
                          let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
                          let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
                          let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
                          if unsafeInterfaceData != nil {
                              let interfaceData = unsafeInterfaceData! as Dictionary!
                              for dictData in interfaceData! {
                                  if dictData.key as! String == "SSID" {
                                      currentSSID = dictData.value as! String
                                  }
                              }
                          }
                      }
                  }
                  return currentSSID
              }
          
            • Re: Find my current SSID in Swift 3 ?
              Laptop_mini Level 1 Level 1 (0 points)

              Here is a cleaner version, which doesn't cycle the entire array before returning:

               

              
                   func fetchSSIDInfo() ->  String? {
                      if let interfaces = CNCopySupportedInterfaces() {
                          for i in 0..<CFArrayGetCount(interfaces){
                              let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
                              let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
                              let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
                             
                              if let unsafeInterfaceData = unsafeInterfaceData as? Dictionary<AnyHashable, Any> {
                                  return unsafeInterfaceData["SSID"] as? String
                              }
                          }
                      }
                      return nil
                  }
              
                • Re: Find my current SSID in Swift 3 ?
                  eskimo Apple Staff Apple Staff (7,960 points)

                  You’re definitely working way too hard here.  The trick with using CF-based APIs from Swift is to get the data into ‘Swift space’ as quickly as possible.  For example:

                  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
                      }
                  }
                  

                  Note that this returns an array of names; how you handle the non-standard cases (no elements, more than one element) is up to you.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: Find my current SSID in Swift 3 ?
                      Wolfpacker Level 1 Level 1 (0 points)

                      Will this work for tvOS as well?  It always returns nil for me.  I have tested on the Apple TV.

                        • Re: Find my current SSID in Swift 3 ?
                          eskimo Apple Staff Apple Staff (7,960 points)

                          Will this work for tvOS as well?

                          I’m quite surprised that the <SystemConfiguration/CaptiveNetwork.h> header is even present in the tvOS SDK.  That header represents a captive network technology that was replaced twice before the first version of tvOS shipped (once via the semi-private captive network plug-ins, and again with NEHotspotHelper).

                          I recommend that you file a bug about this.  Either that API should be made to work on tvOS, or it should be marked up to indicate that it’s not available on tvOS.

                          Please post your bug number, just for the record.

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"

                        • Re: Find my current SSID in Swift 3 ?
                          Philly_Photo Level 1 Level 1 (0 points)

                          Is there a macOS equivalent of this, since CNCopyCurrentNetworkInfo is available only on iOS and tvOS?

                            • Re: Find my current SSID in Swift 3 ?
                              eskimo Apple Staff Apple Staff (7,960 points)

                              macOS has CoreWLAN, a comprehensive public API for interacting with Wi-Fi.  Here’s how you’d implement my currentSSIDs() function using it.

                              import CoreWLAN
                              
                              func currentSSIDs() -> [String] {
                                  let client = CWWiFiClient.shared()
                                  return client.interfaces()?.flatMap { interface in
                                      return interface.ssid()
                                  } ?? []
                              }

                              Share and Enjoy

                              Quinn “The Eskimo!”
                              Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                              let myEmail = "eskimo" + "1" + "@apple.com"

                                • Re: Find my current SSID in Swift 3 ?
                                  claoudj Level 1 Level 1 (0 points)

                                  Hi eskimo, I have a question about the BSSID,

                                  I use the same methods as yours but I replaced "kCNNetworkInfoKeySSID" by "kCNNetworkInfoKeyBSSID".

                                  And I have an unexpected result.

                                  I received: ["b0:95:9e:9c:2a:6"] and should receive ["b0:95:9e:9c:2a:06"]

                                   

                                  Do you have any clue about why the bssid isn't complete ?

                                  Thx in advance ! have a good day !

                                    • Re: Find my current SSID in Swift 3 ?
                                      eskimo Apple Staff Apple Staff (7,960 points)

                                      Do you have any clue about why the bssid isn't complete ?

                                      It seems that the only difference is that the BSSID is missing leading 0 digits.  While that’s not something I do myself, it’s a relatively common practice for MAC addresses.  If you need the leading 0, you’ll need to fix that yourself.  For example:

                                      func normalise(macString: String) -> String {
                                          return String(macString
                                              .uppercased()
                                              .split(separator: ":")
                                              .map{("00" + $0).suffix(2)}
                                              .joined(separator: ":")
                                          )
                                      }
                                      print(normalise(macString: "b0:95:9e:9c:2a:06"))    // prints: B0:95:9E:9C:2A:06
                                      print(normalise(macString: "b0:95:9e:9c:2a:6"))     // prints: B0:95:9E:9C:2A:06

                                      Share and Enjoy

                                      Quinn “The Eskimo!”
                                      Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                      let myEmail = "eskimo" + "1" + "@apple.com"