iOS 13 - CNCopyCurrentNetworkInfo

1. 获取iPhone当前WiFi name ,又又又变了

iOS 13后 增加了需要获取位置权限

相应的在plist里增加获取位置信息的描述参数也是必不可少


iOS 13 - CNCopyCurrentNetworkInfo_第1张图片

-(NSDictionary *)getWifiInfo{

    if(@available(iOS13.0, *)) {

        //用户明确拒绝,可以弹窗提示用户到设置中手动打开权限

        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {

            NSLog(@"User has explicitly denied authorization for this application, or location services are disabled in Settings.");

            //使用下面接口可以打开当前应用的设置页面

            //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

            returnnil;

        }

        CLLocationManager* cllocation = [[CLLocationManager alloc] init];

        if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){

            //弹框提示用户是否开启位置权限

            [cllocationrequestWhenInUseAuthorization];

            usleep(50);

            //递归等待用户选选择

            return[selfgetWifiInfo];

        }

    }

    NSString*wifiName =nil;

    NSDictionary*info =nil;

    CFArrayRefwifiInterfaces =CNCopySupportedInterfaces();

    if(!wifiInterfaces) {

        returnnil;

    }

    NSArray*interfaces = (__bridgeNSArray*)wifiInterfaces;

    for(NSString*interfaceNameininterfaces) {

        CFDictionaryRefdictRef =CNCopyCurrentNetworkInfo((__bridgeCFStringRef)(interfaceName));

        if(dictRef) {

            info = (__bridgeNSDictionary*)dictRef;

            NSLog(@"network info -> %@", info);

            wifiName = [infoobjectForKey:(__bridgeNSString*)kCNNetworkInfoKeySSID];

            CFRelease(dictRef);

        }

    }

    CFRelease(wifiInterfaces);

    return info;

更多参考 asml ,作者 juejin

你可能感兴趣的:(iOS 13 - CNCopyCurrentNetworkInfo)