iPhone get SSID without private library

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

    For iOS 13

    As from iOS 13 your app also needs Core Location access in order to use the CNCopyCurrentNetworkInfo function unless it configured the current network or has VPN configurations:

    So this is what you need (see apple documentation):
    - Link the CoreLocation.framework library
    - Add location-services as a UIRequiredDeviceCapabilities Key/Value in Info.plist
    - Add a NSLocationWhenInUseUsageDescription Key/Value in Info.plist describing why your app requires Core Location
    - Add the "Access WiFi Information" entitlement for your app

    Now as an Objective-C example, first check if location access has been accepted before reading the network info using CNCopyCurrentNetworkInfo:

    - (void)fetchSSIDInfo {
        NSString *ssid = NSLocalizedString(@"not_found", nil);
    
        if (@available(iOS 13.0, *)) {
            if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
                NSLog(@"User has explicitly denied authorization for this application, or location services are disabled in Settings.");
            } else {
                CLLocationManager* cllocation = [[CLLocationManager alloc] init];
                if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
                    [cllocation requestWhenInUseAuthorization];
                    usleep(500);
                    return [self fetchSSIDInfo];
                }
            }
        }
    
        NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
        id info = nil;
        for (NSString *ifnam in ifs) {
            info = (__bridge_transfer id)CNCopyCurrentNetworkInfo(
                (__bridge CFStringRef)ifnam);
    
            NSDictionary *infoDict = (NSDictionary *)info;
            for (NSString *key in infoDict.allKeys) {
                if ([key isEqualToString:@"SSID"]) {
                    ssid = [infoDict objectForKey:key];
                }
            }
        }        
            ...
        ...
    }
    

提交回复
热议问题