Cncopycurrentnetworkinfo with iOS 13

How to fetch SSID in iOS device with iOS 13

Using the code provided on iOS 14 I got the following error:

nehelper sent invalid result code [1] for Wi-Fi information request

Searching for that error took me to this question

Solution:

The requesting app must meet one of the following requirements:

The app uses Core Location, and has the user’s authorization to use
location information.

The app uses the NEHotspotConfiguration API to configure the current
Wi-Fi network.

The app has active VPN configurations installed.

An app that fails to meet any of the above requirements receives the
following return value:

An app linked against iOS 12 or earlier receives a dictionary with
pseudo-values. In this case, the SSID is Wi-Fi (or WLAN in the China
region), and the BSSID is 00:00:00:00:00:00.

An app linked against iOS 13 or later receives NULL.

Important

To use this function, an app linked against iOS 12 or later must
enable the Access WiFi Information capability in Xcode.

I also confirmed that this in fact works on iOS 14 once you request location permission.

import CoreLocation
import UIKit
import SystemConfiguration.CaptiveNetwork

final class ViewController: UIViewController {
var locationManager: CLLocationManager?

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestAlwaysAuthorization()
}

func getWiFiName() -> String? {
var ssid: String?

if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
}
}
}

return ssid
}
}

extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways || status == .authorizedAlways {
let ssid = self.getWiFiName()
print("SSID: \(String(describing: ssid))")
}
}
}

Output: SSID: YaMomsWiFi

Don't forget to include the wifi entitlement, and the necessary keys in your plist for location permission.

iOS 13 wifi ssid in airplane mode

In iOS13, and possibly earlier versions (that I do not have immediately available to test on), once you enable "Airplane mode," the WiFi is automatically disconnected.

An end-user would need to proactively reenable WiFi on their device to reconnect, while still having Airplane mode enabled.

Your if statement isn't getting executed, likely because there's no network information yet. Reenabling WiFi should get you the expected results.

Getting the SSID of the CONNECTED WIFI on IOS 12- Xamarin (Updated for iOS 13)

To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app .Sample Image

So you have to check Access WiFi Infomation in appid.

Sample Image

iPhone get SSID without private library

As of iOS 7 or 8, you can do this (need Entitlement for iOS 12+ as shown below):

@import SystemConfiguration.CaptiveNetwork;

/** Returns first non-empty SSID network info dictionary.
* @see CNCopyCurrentNetworkInfo */
- (NSDictionary *)fetchSSIDInfo {
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);

NSDictionary *SSIDInfo;
for (NSString *interfaceName in interfaceNames) {
SSIDInfo = CFBridgingRelease(
CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
NSLog(@"%s: %@ => %@", __func__, interfaceName, SSIDInfo);

BOOL isNotEmpty = (SSIDInfo.count > 0);
if (isNotEmpty) {
break;
}
}
return SSIDInfo;
}

Example output:

2011-03-04 15:32:00.669 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: Supported interfaces: (
en0
)
2011-03-04 15:32:00.693 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: en0 => {
BSSID = "ca:fe:ca:fe:ca:fe";
SSID = XXXX;
SSIDDATA = <01234567 01234567 01234567>;
}

Note that no ifs are supported on the simulator. Test on your device.

iOS 12

You must enable access wifi info 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

Swift 4.2

func getConnectedWifiInfo() -> [AnyHashable: Any]? {

if let ifs = CFBridgingRetain( CNCopySupportedInterfaces()) as? [String],
let ifName = ifs.first as CFString?,
let info = CFBridgingRetain( CNCopyCurrentNetworkInfo((ifName))) as? [AnyHashable: Any] {

return info
}
return nil

}

Detect the name of wireless network

This worked perfect for me:

#import <SystemConfiguration/CaptiveNetwork.h>

CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
// NSLog(@"SSID: %@",CFDictionaryGetValue(myDict, kCNNetworkInfoKeySSID));
NSString *networkName = CFDictionaryGetValue(myDict, kCNNetworkInfoKeySSID);

if ([networkName isEqualToString:@"Hot Dog"])
{
self.storeNameController = [[StoreDataController alloc] init];
[self.storeNameController addStoreNamesObject];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Connection Failed"
message: @"Please connect to the Hot Dog network and try again"
delegate: self
cancelButtonTitle: @"Close"
otherButtonTitles: nil];

[alert show];


Related Topics



Leave a reply



Submit