如果关闭位置服务,CLLocationManager永远不会更新

Jam*_*mes 2 iphone core-location ios

如果用户已关闭位置服务(针对所有应用程序或仅针对我的应用程序),则CLLocationManager永远不会提示并且永远不会更新位置.这是可以预料的,但有没有办法检测位置服务是否已关闭,并提示重新启用它?

Tim*_*han 9

根据文档,CLLocationManager有一个静态方法来查看是否启用了LocationServices:

+ (BOOL)locationServicesEnabled
Run Code Online (Sandbox Code Playgroud)


mat*_*atm 5

只是为了完成蒂姆的回答.

您始终可以尝试通过CLLocationManager实例上的startUpdatingLocation方法启动位置更新.如果禁用位置服务,您将通过委托获得有关错误情况的通知,然后您可以打开对话框,要求用户进入"设置"并为您的应用启用位置服务...

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)inManager didFailWithError:(NSError *)inError{
    if (inError.code ==  kCLErrorDenied) {
        NSLog(@"Location manager denied access - kCLErrorDenied");
        // your code to show UIAlertView telling user to re-enable location services
        // for your app so they can benefit from extra functionality offered by app
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您可以通过iOS5上的URL-scheme启动"设置"应用程序(版本低于5.0(且大于5.0),不支持).呼叫:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];
Run Code Online (Sandbox Code Playgroud)