美文网首页
ios 通过AFNetworking来判断是否连上wifi

ios 通过AFNetworking来判断是否连上wifi

作者: 今年27 | 来源:发表于2019-12-26 16:52 被阅读0次

如果不要求必须知道wifi名称的话,这样就可以了

      if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWiFi) {
            return YES;
        }else{
            return NO;
        }

如果需要知道wifi名字, 在ios13之后必须开启location权限
如下

#import <CoreLocation/CoreLocation.h>

-(void)getLocation{
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    CGFloat version = [phoneVersion floatValue];
    // 如果是iOS13 未开启地理位置权限 需要提示一下
    if ( version >= 13) {
       self.locationManager = [[CLLocationManager alloc] init];
       [self.locationManager requestWhenInUseAuthorization];
    }
}

+ (NSString *)getWifiName
{
    NSString *wifiName = nil;
    
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    
    if (!wifiInterfaces) {
        return nil;
    }
    
    NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
    
    for (NSString *interfaceName in interfaces) {
        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
        
        if (dictRef) {
            NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
//            NSLog(@"network info -> %@", networkInfo);
            wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
            
            CFRelease(dictRef);
        }
    }
    
    CFRelease(wifiInterfaces);
    return wifiName;
}

info.plist文件

        <key>NSLocationAlwaysUsageDescription</key>
    <string>open the location to get the wifi ssid</string>
    <key>NSLocationUsageDescription</key>
    <string>open the location to get the wifi ssid</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>open the location to get the wifi ssid</string>

相关文章

网友评论

      本文标题:ios 通过AFNetworking来判断是否连上wifi

      本文链接:https://www.haomeiwen.com/subject/qblmnctx.html