[英]iOS, CLLocationManager: Displaying the “reenable Location Services” Popup
[英]CLLocationManager doesn't show popup if location services were previously disabled
当我启动我的应用程序时,我检查当前位置授权状态如下:
- (void)checkCurrentStatus
{
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
[self.locationManager requestWhenInUseAuthorization];
}
else
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied && ![CLLocationManager locationServicesEnabled])
{
[self.locationManager startUpdatingLocation];
}
}
如果启用整体位置服务(对于整个设备)而不是仅仅要求用户许可,则会弹出警报。 如果它们被禁用(否则如果条件)那么我需要调用startUpdatingLocation
因为调用[self.locationManager requestWhenInUseAuthorization];
授权状态为kCLAuthorizationStatusDenied
(没有任何if条件)时kCLAuthorizationStatusDenied
。 好的,所以我调用startUpdatingLocation
,然后弹出警告说:
启用位置服务以允许“AppName”确定您的位置
好的,我转到整体位置服务的设置。 在此之后,authorizationStatus变为kCLAuthorizationStatusNotDetermined
但是当我调用requestWhenInUseAuthorization
它没有任何效果! 没有弹出窗口,用户没有提示授权位置,状态保持不变,我无法使用位置管理器。 我该怎么处理?
来自Apple的CLLocationManager上的文档- (void)requestWhenInUseAuthorization
如果当前授权状态不是kCLAuthorizationStatusNotDetermined,则此方法不执行任何操作并且不调用locationManager:didChangeAuthorizationStatus:方法
这就是你需要的:
- (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];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.