iOS get current wlan network name

32,347

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.

Share:
32,347

Related videos on Youtube

Chris
Author by

Chris

Updated on July 09, 2022

Comments

  • Chris
    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

  • Chris
    Chris almost 12 years
    Thanks. I have not thought about searching for "SSID"... Works perfectly!
  • Freddy
    Freddy over 8 years
    This is deprecated in iOS 9.0. Is there a new way?
  • Rok Jarc
    Rok Jarc over 8 years
    I didn't try this but seems legit: registering your app as a hot spot helper: stackoverflow.com/a/31590787/653513
  • Rok Jarc
    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