当我启动我的应用时,我会检查当前位置授权状态:
- (void)checkCurrentStatus
{
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
[self.locationManager requestWhenInUseAuthorization];
}
else
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied && ![CLLocationManager locationServicesEnabled])
{
[self.locationManager startUpdatingLocation];
}
}
如果启用了整体位置服务(对于整个设备)而不是仅仅要求用户许可,则会弹出警报。如果他们被禁用(否则如果条件),那么我需要致电startUpdatingLocation
,因为当授权状态为[self.locationManager requestWhenInUseAuthorization];
时(无任何条件),对kCLAuthorizationStatusDenied
的调用无效。好的,所以我打电话给startUpdatingLocation
,然后提醒弹出说:
启用位置服务以允许“AppName”确定您的位置
好的,我转到整体位置服务的设置。在此之后,授权状态变为kCLAuthorizationStatusNotDetermined
,但当我致电requestWhenInUseAuthorization
时,它无效!没有弹出窗口,用户没有提示授权位置,状态保持不变,我无法使用位置管理器。我该怎么处理?
答案 0 :(得分:5)
来自Apple关于CLLocationManager的文档关于- (void)requestWhenInUseAuthorization
如果当前授权状态不是 kCLAuthorizationStatusNotDetermined,这个方法什么都不做 不要调用locationManager:didChangeAuthorizationStatus:method
以下是您的需求:
- (void)requestAlwaysAuthorization
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
NSString *title;
title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Settings", nil];
[alertView show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestAlwaysAuthorization];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Send the user to the Settings for this app
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}
}