iPhone get SSID without private library

后端 未结 9 946
[愿得一人]
[愿得一人] 2020-11-22 06:00

I have a commercial app that has a completely legitimate reason to see the SSID of the network it is connected to: If it is connected to a Adhoc network for a 3rd party har

9条回答
  •  梦如初夏
    2020-11-22 06:30

    UPDATE FOR iOS 10 and up

    CNCopySupportedInterfaces is no longer deprecated in iOS 10. (API Reference)

    You need to import SystemConfiguration/CaptiveNetwork.h and add SystemConfiguration.framework to your target's Linked Libraries (under build phases).

    Here is a code snippet in swift (RikiRiocma's Answer):

    import Foundation
    import SystemConfiguration.CaptiveNetwork
    
    public class SSID {
        class func fetchSSIDInfo() -> String {
            var currentSSID = ""
            if let interfaces = CNCopySupportedInterfaces() {
                for i in 0.. = CFArrayGetValueAtIndex(interfaces, i)
                    let rec = unsafeBitCast(interfaceName, AnyObject.self)
                    let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
                    if unsafeInterfaceData != nil {
                        let interfaceData = unsafeInterfaceData! as Dictionary!
                        currentSSID = interfaceData["SSID"] as! String
                    }
                }
            }
            return currentSSID
        }
    }
    

    (Important: CNCopySupportedInterfaces returns nil on simulator.)

    For Objective-c, see Esad's answer here and below

    + (NSString *)GetCurrentWifiHotSpotName {    
        NSString *wifiName = nil;
        NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
        for (NSString *ifnam in ifs) {
            NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
            if (info[@"SSID"]) {
                wifiName = info[@"SSID"];
            }
        }
        return wifiName;
    }
    

    UPDATE FOR iOS 9

    As of iOS 9 Captive Network is deprecated*. (source)

    *No longer deprecated in iOS 10, see above.

    It's recommended you use NEHotspotHelper (source)

    You will need to email apple at networkextension@apple.com and request entitlements. (source)

    Sample Code (Not my code. See Pablo A's answer):

    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;
    }
    

    Side note: Yup, they deprecated CNCopySupportedInterfaces in iOS 9 and reversed their position in iOS 10. I spoke with an Apple networking engineer and the reversal came after so many people filed Radars and spoke out about the issue on the Apple Developer forums.

提交回复
热议问题