3 Replies
      Latest reply: Dec 13, 2016 8:00 AM by eskimo 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 (6,260 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"