如果不要求必须知道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>
网友评论