问题
in iOS 10, sometimes when install the app, location permission prompts opens lots of time and hangs all app and not able to move further.
here is my code that works before 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 ){
}
}
}
In my plist file,
<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>
回答1:
Regardless of iOS 10, you should start your location updating only if the permission was granted, you should also check if the permission is already granted before requesting permissions:
-(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;
}
}
回答2:
May be you can try below checks and see if it helps:
Do not call requestWhenInUseAuthorization
every time.
check for both locationServicesEnabled
and authorizationStatus
, call for requestWhenInUseAuthorization
only if authorizationStatus
is kCLAuthorizationStatusDenied
and locationServicesEnabled
return false
.
Like,
if(![CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
}
Hope it will help:)
来源:https://stackoverflow.com/questions/39695201/location-permission-dialog-prompts-lots-of-time-in-ios-10