iPhone get SSID without private library

后端 未结 9 791
[愿得一人]
[愿得一人] 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:43

    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
    
    }
    

提交回复
热议问题