Skip to content

Instantly share code, notes, and snippets.

@werediver
Created July 14, 2016 12:47
Show Gist options
  • Save werediver/b1d5ed625a3fbf538481e40a71b4d1ac to your computer and use it in GitHub Desktop.
Save werediver/b1d5ed625a3fbf538481e40a71b4d1ac to your computer and use it in GitHub Desktop.
Get the connected Wi-Fi network SSID on iOS (in Swift).
import Foundation
import SystemConfiguration.CaptiveNetwork
func getWiFiSsid() -> 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
}
@Martius108
Copy link

works fine for an iOS App, but when I use this code in an MacOSX App, Xcode says that CNCopyCurrentNetworkInfo is unavailable. What is the reason for this?

@werediver
Copy link
Author

@Martius108 Unfortunately, CNCopyCurrentNetworkInfo function is available on iOS only. For macOS you need to find other way to get info about the connected Wi-Fi network.

@RobertChalmers
Copy link

What other information could I get from this apart from the ssid?

@mhmdzaid
Copy link

mhmdzaid commented May 13, 2018

hey i am currently developing ios apps on virtual machine so the SSID is nil
is that should be tested on i phone or what ??
@werediver

@manucodin
Copy link

Im trying in iOS 12 with physical device and it happens to me like @mohamedzead1996. iOS 12 bug?

@MihaiPantiru
Copy link

@MRodSebastian On iOS 12 I got nil first, but after turning on the new "Access WiFi Information" in Capabilities is working fine.

@shivareddy
Copy link

Does this capability is required for iOS 12 or Xcode 10? I mean, will it work on iOS12 if we build using Xcode 9 and submit app to AppStore?

@eschos24
Copy link

Thanks, @MihaiPantiru! That worked great for me.

@ioio007
Copy link

ioio007 commented Sep 18, 2018

@MihaiPantiru thanks! That solved my problem.
@shivareddy It's required for Xcode 10, since it works fine before I update to Xcode 10.

@lenguyenvu007
Copy link

Thank you, @MihaiPantiru that solved the problem, when I updated to Xcode 10, iOS12.

@hungharry123
Copy link

I have issue "does not support the Access WiFi Information capability". How to fix it ? @MihaiPantiru

@BB9z
Copy link

BB9z commented Oct 18, 2018

A little tidy up.

extension UIDevice {
    @objc var WiFiSSID: String? {
        guard let interfaces = CNCopySupportedInterfaces() as? [String] else { return nil }
        let key = kCNNetworkInfoKeySSID as String
        for interface in interfaces {
            guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as CFString) as NSDictionary? else { continue }
            return interfaceInfo[key] as? String
        }
        return nil
    }
}

@donly
Copy link

donly commented Jun 11, 2019

Not work for iOS 13 anymore

@cmooreml
Copy link

Is their any valid way to get SSID in iOS 13?

@donly
Copy link

donly commented Jun 13, 2019

@AlexFlyce
Copy link

AlexFlyce commented Oct 10, 2019

Even with the core location authorisations and the wifi capability, I'm not able to retrieve the SSID via the CNCopyCurrentNetworkInfo (as reported here: https://github.com/HackingGate/iOS13-WiFi-Info)
Did anyone get the same problem?

@WilliamCYChan
Copy link

I got the sample problem @AlexFlyce. After I upgrade iOS13 and XCode 11.1, CNCopyCurrentNetworkInfo return NULL. It haven't any problem before my upgrade. Did anyone have solve the problem ?

@AlexFlyce
Copy link

Apparently it's a known issue from Apple side, and the best we can do is to submit more logs of the bug. (PS: If you reboot your phone, the bug won't appear anymore, so it's not even easy to reproduce at that point)
My biggest problem is that my app relies completely on this, and I'm getting bad reviews...

@nneuberger1
Copy link

nneuberger1 commented Dec 18, 2019

After installing the 13.3 GM release, I was able to get the command to working.

I was also able to get the FGRoute pod to work fine except for the FGRoute.getSSIDDATA() function as it's causing my application to crash, although I don't need that function.

https://github.com/Feghal/FGRoute

@dineshgupthabavirisetti
Copy link

dineshgupthabavirisetti commented Jul 23, 2020

Hi Guys I have to validate the password of connected Wifi Router. am able to get the SSID But I need validation password also to validate User Entered password is correct or not.

@vipulav-j
Copy link

Hi Guys I have to validate the password of connected Wifi Router. am able to get the SSID But I need validation password also to validate User Entered password is correct or not.

How you are able to get the connected Wifi Router name? I have iOS 17.4.1 and i want to display connected wifi name in my react native app. Can you please share details ?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment