2

I am using the below code to retrieve the SSID of the WiFi network the iPod is connected.

NSArray *ifs = (id)CNCopySupportedInterfaces();
NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
id info = nil;
for (NSString *ifnam in ifs) {
    info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
    NSLog(@"%s: %@ => %@", __func__, ifnam, info);
    if (info && [info count]) {
        break;
    }
    [info release];
}

Sometimes this code is not returning the proper SSID of the network my device is connected.Any pointers on why the SSID is not retrieved correctly? Does CNCopyCurrentNetworkInfo package dependent on the iOS version of the device?

Thanks.

Cooldude
  • 31
  • 3
  • 9

3 Answers3

6
  1. add SystemConfiguration.framework to project.

  2. import < SystemConfiguration/CaptiveNetwork.h >

  3. CFArrayRef myArray = CNCopySupportedInterfaces();
    CFStringRef interfaceName = CFArrayGetValueAtIndex(myArray, 0);
    CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(interfaceName);
    NSDictionary *dict = ( NSDictionary*) captiveNtwrkDict;
    NSString* ssid = [dict objectForKey:@"SSID"];
    NSLog(@"%s ssid : %@",__FUNCTION__, [ssid description]);
    
  4. For iOS 12 and later, you must enable it from capabilities.

Important 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. Documentation link

Khaled Annajar
  • 14,364
  • 5
  • 33
  • 44
vualoaithu
  • 938
  • 10
  • 9
0

Yes. CNCopyCurrentNetworkInfo is available only in iOS 4.1 and later.

For more info ,please look at the developer.apple SystemConfiguration Reference

you can check the sample code here

Community
  • 1
  • 1
Shamsudheen TK
  • 29,791
  • 9
  • 67
  • 99
0

Starting with iOS 13, the CNCopyCurrentNetworkInfo API will no longer return valid Wi-Fi SSID and BSSID information. Instead, the information returned by default will be:

SSID: “Wi-Fi” or “WLAN” (“WLAN" will be returned for the China SKU) BSSID: "00:00:00:00:00:00"

If your app is using this API, Apple now encourage you to adopt alternative approaches that don’t require Wi-Fi or network information. Valid SSID and BSSID information from CNCopyCurrentNetworkInfo will still be provided to VPN apps, apps that have used NEHotspotConfiguration to configure the current Wi-Fi network, and apps that have obtained permission to access user location through Location Services.

You can Test your app on the latest iOS 13 beta to make sure it works properly. If your app requires valid Wi-Fi SSID and BSSID information to function, you can do the following: For accessory setup apps, use the NEHotSpotConfiguration API, which now has the option to pass a prefix of the SSID hotspot your app expects to connect to. For other types of apps, use the CoreLocation API to request the user’s consent to access location information.

Lakshay Sharma
  • 1,485
  • 12
  • 12
  • You need the localtion permission on iOS 13, then, you can retrive the ssid. Note that there is a bug in iOS13 and sometimes even with location permission, you cannot retrieve the ssid. (iOS 13.1.2) – Ricardo Oct 08 '19 at 10:50
  • @RicardoYes you're right in my app I'm using Location access though not able to get CNCopyCurrentNetworkInfo is returning nil. Any suggestion how to access wifi info in ios 13 or above ? – Parth Barot Jan 21 '20 at 10:20