这两天,测试机升级到iOS13beta6 版本,发现获取不到WiFi 的ssid了,之前通过CNCopyCurrentNetworkInfo 获取 ssid不好使了。
通过 CNCopyCurrentNetworkInfo 一直都返回 “WANL”
这到底是怎么回事呢?
一通折腾以后,终于找到原因了
下面着重讲一下最简单的开启定位权限
方法:
1. 添加CoreLocation.framework
2.#import <CoreLocation/CLLocationManager.h>
3.创建@property (nonatomic, strong) CLLocationManager *locManager;
属性
4.设置定位权限
其中:self.locManager = [[CLLocationManager alloc] init];
如果不使用属性,直接CLLocationManager *locManager = [[CLLocationManager alloc] init];
,出了方法locManager
就会被释放,授权弹框就会一出现就消失(闪现)
- (void)getcurrentLocation {
if (@available(iOS 13.0, *)) {
//用户明确拒绝,可以弹窗提示用户到设置中手动打开权限
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
//使用下面接口可以打开当前应用的设置页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
//弹框提示用户是否开启位置权限
[self.locManager requestWhenInUseAuthorization];
}
}
}
5.接收代理self.locManager.delegate = self;
,实现代理方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse ||
status == kCLAuthorizationStatusAuthorizedAlways) {
//再重新获取ssid
self.wifiSsidTf.text = [self wifSsid];
}
}
+ (NSString*)wifSsid
{
NSArray *interfaces = (__bridge_transfer NSArray*)CNCopySupportedInterfaces();
NSDictionary *info = nil;
for (NSString *ifname in interfaces) {
info = (__bridge_transfer NSDictionary*)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
if (info && [info count]) {
break;
}
info = nil;
}
NSString *ssid = nil;
if ( info ){
ssid = [info objectForKey:@"SSID"];
}
info = nil;
return ssid? ssid:@"";
}