[英]Location permission dialog prompts lots of time in iOS 10
在iOS 10中,有时在安装应用程序时,位置权限提示会打开大量时间并挂起所有应用程序而无法进一步移动。
这是我的代码在iOS 10之前有效
-(void)startLocationManager{
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.delegate=self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
if (self.myCurrentLocation==nil) {
self.myCurrentLocation=[locations lastObject];
[[WALocationManager WALocationSharedInstance] checkLatestLocation];
}
else{
if (self.myCurrentLocation.horizontalAccuracy < 0){
return;
}
self.myCurrentLocation=[locations lastObject];
if([[WALocationManager WALocationSharedInstance] currentLocation]!=self.myCurrentLocation ){
}
}
}
在我的plist文件中,
<key>NSLocationAlwaysUsageDescription</key>
<string>This app will use your location to get most nearyby activity around you.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app will use your location.</string>
无论iOS 10如何,只有在授予权限的情况下才应启动位置更新,还应在请求权限之前检查是否已授予权限:
-(void)startLocationManager{
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.delegate=self;
// Check for current permissions
[self checkLocationAuth:[CLLocationManager authorizationStatus]];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
[self checkLocationAuth:status];
}
-(void)checkLocationAuth:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways:
[self.locationManager startUpdatingLocation];
break;
// did not ask for permission, ask now
case kCLAuthorizationStatusNotDetermined:
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
} else { // iOS < 8? implicitly request permission
[self.locationManager startUpdatingLocation];
}
break;
// Also need to handle failures, etc
default:
break;
}
}
也许你可以尝试下面的检查,看看它是否有帮助:
每次都不要调用requestWhenInUseAuthorization
。
检查locationServicesEnabled
和authorizationStatus
,仅当authorizationStatus
为kCLAuthorizationStatusDenied
并且locationServicesEnabled
返回false
时才调用requestWhenInUseAuthorization
。
喜欢,
if(![CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
}
希望它会有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.