18

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
  • 3,057
  • 5
  • 37
  • 63

3 Answers3

17

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

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • 2
    Thanks. I have not thought about searching for "SSID"... Works perfectly! – Chris Jun 10 '12 at 09:41
  • 1
    This is deprecated in iOS 9.0. Is there a new way? – Freddy Jan 13 '16 at 18:23
  • I didn't try this but seems legit: registering your app as a hot spot helper: http://stackoverflow.com/a/31590787/653513 – Rok Jarc Jan 14 '16 at 06:57
  • @OhadM: the answer you had linked also uses deprecated framework. But there is another answer on same 'thread': http://stackoverflow.com/a/33132529/653513 – Rok Jarc Feb 17 '16 at 11:01
  • @rokjarc I have tested it and it doesn't work. It requires Apple's entitlements. Kindly test the code snip and you shall see. Also, you are correct, it is deprecated but still works. – OhadM Feb 17 '16 at 12:02
12

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"]];
}
nuynait
  • 1,912
  • 20
  • 27
10

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.

Derek
  • 1,011
  • 6
  • 17