How to get Wifi SSID in iOS9 after CaptiveNetwork is deprecated and calls for Wifi name are already blocked
Until today I used the CaptiveNetwork Interface to display the name of the currently connected Wifi. The iOS 9 Prerelease reference already stated, that the CaptiveNetwork methods are depracted now, but they still worked at the beginning.
With the newest version Apple seems to have blocked this calls already (maybe due to privacy concerns?).
Is there any other way to get the name of the current Wifi?
This is how I obtained the SSID until today, but you only get nil now:
#import <SystemConfiguration/CaptiveNetwork.h>
NSString *wifiName = nil;
NSArray *interFaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *name in interFaceNames) {
NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name);
if (info[@"SSID"]) {
wifiName = info[@"SSID"];
}
}
Register your app as Hotspot helper.
#import <NetworkExtension/NetworkExtension.h>
NSArray * networkInterfaces = [NEHotspotHelper supportedNetworkInterfaces];
NSLog(@"Networks %@",networkInterfaces);
UPDATE (Sept. 11th, 2015)
The following Captive Network APIs have been re-enabled in the latest version of iOS 9 instead.
- CNCopySupportedInterfaces
- CNCopyCurrentNetworkInfo
UPDATE (Sept. 16th, 2015)
If you still prefer to use NetworkExtension
and Apple gave you permission to add the entitlements, then you can do this to get the wifi information:
for(NEHotspotNetwork *hotspotNetwork in [NEHotspotHelper supportedNetworkInterfaces]) {
NSString *ssid = hotspotNetwork.SSID;
NSString *bssid = hotspotNetwork.BSSID;
BOOL secure = hotspotNetwork.secure;
BOOL autoJoined = hotspotNetwork.autoJoined;
double signalStrength = hotspotNetwork.signalStrength;
}
NetworkExtension
provides you some extra information as secure, auto joined or the signal strength. And it also allows you to set credential to wifis on background mode, when the user scans wifis around.
In the GM for iOS 9, it seems like this is enabled again. In fact, it's not even listed as deprecated in the online documentation, however the CaptiveNetwork header file does have the following:
CNCopySupportedInterfaces (void) __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_8, __MAC_NA, __IPHONE_4_1, __IPHONE_9_0, CN_DEPRECATION_NOTICE);
So, it is working in the iOS 9 GM, but not sure for how long :)
The answer by abdullahselek is still correct even for Swift 4.1 and 4.2.
A small caveat is that now in iOS 12, you must go to the capabilities section of your app project and enable the Access WiFi Information feature. It will add an entitlement entry to your project and allow the function call CNCopyCurrentNetworkInfo
to work properly.
If you do not do this, that function simply returns nil. No errors or warnings at runtime about the missing entitlement will be displayed.
For more info, see the link below to Apple's documentation.
https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo