iOS get current wlan network name
Solution 1
From iOS >= 4.1 it's possible to obtain SSID of wireless network that device is currenctly connected to.
For this you'd use function CNCopyCurrentNetworkInfo
Details on implemenation are available on SO: iPhone get SSID without private library
Solution 2
It is possible to get the current wifi information from the Captive Network. In the past, apple actually disabled this for a while, but they seems to re-enabled it due to strong request. It is also possible that they decide to close this in the future.
The information we can get is BSSID
, SSID
, SSIDDATA
. BSSID
is the unique address for wifi, SSID
is the current wifi name, SSIDDATA
is the hex representation for the SSID
.
For Swift 3.1:
func printCurrentWifiInfo() {
if let interface = CNCopySupportedInterfaces() {
for i in 0..<CFArrayGetCount(interface) {
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interface, i)
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
if let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString), let interfaceData = unsafeInterfaceData as? [String : AnyObject] {
// connected wifi
print("BSSID: \(interfaceData["BSSID"]), SSID: \(interfaceData["SSID"]), SSIDDATA: \(interfaceData["SSIDDATA"])")
} else {
// not connected wifi
}
}
}
}
For Objective-C
NSArray *interFaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *name in interFaceNames) {
NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name);
NSLog[@"wifi info: bssid: %@, ssid:%@, ssidData: %@", info[@"BSSID"], info[@"SSID"], info[@"SSIDDATA"]];
}
Solution 3
As of iOS 12, you'll need to allow Wifi Information access in the capabilities.
From Apple:
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.
Related videos on Youtube
Chris
Updated on July 09, 2022Comments
-
Chris almost 2 years
I am looking for a way to obtain information (at least the name) of the current connected wlan network in objective-c for iOS5.
I need this because we are currently developing an application that do not work in a particular network. In this network (on our university) the port is closed that we need to connect to the server. But there is another network also available and we want to tell the user that he has to switch the network if he is connected to the aforementioned one.
I do not even know where to start. Does anyone have an idea or any hints?
Thanks and regards
-
Rok Jarc almost 12 yearshere you go: stackoverflow.com/questions/5198716/…
-
-
Chris almost 12 yearsThanks. I have not thought about searching for "SSID"... Works perfectly!
-
Freddy over 8 yearsThis is deprecated in iOS 9.0. Is there a new way?
-
Rok Jarc over 8 yearsI didn't try this but seems legit: registering your app as a hot spot helper: stackoverflow.com/a/31590787/653513
-
Rok Jarc about 8 years@OhadM: the answer you had linked also uses deprecated framework. But there is another answer on same 'thread': stackoverflow.com/a/33132529/653513