在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>
答案 0 :(得分:1)
无论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;
}
}
答案 1 :(得分:0)
可能您可以尝试以下检查,看看是否有帮助:
不要每次都致电requestWhenInUseAuthorization
。
检查locationServicesEnabled
和authorizationStatus
,仅当requestWhenInUseAuthorization
为authorizationStatus
且kCLAuthorizationStatusDenied
返回locationServicesEnabled
时才需要false
像,
if(![CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
}
希望它会有所帮助:)