简体   繁体   English

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

[英]If location services is turned off, CLLocationManager never updates

If a user has turned off location services (either for all apps or just mine), CLLocationManager never prompts and never updates the position. 如果用户已关闭位置服务(针对所有应用程序或仅针对我的应用程序),则CLLocationManager永远不会提示并且永远不会更新位置。 This is to be expected, but is there a way to detect that location services is turned off, and prompt to reenable it? 这是可以预料的,但有没有办法检测位置服务是否已关闭,并提示重新启用它?

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

+ (BOOL)locationServicesEnabled

Just to complete Tim's answer. 只是为了完成蒂姆的回答。

You can always try starting location updates via startUpdatingLocation method on CLLocationManager instance. 您始终可以尝试通过CLLocationManager实例上的startUpdatingLocation方法启动位置更新。 If location services are disabled, you'll get notified about error condition by delegate and then you can bring up the dialog asking user to go into Settings and enable location services for your app... 如果禁用位置服务,您将通过委托获得有关错误情况的通知,然后您可以打开对话框,要求用户进入“设置”并为您的应用启用位置服务...

#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
    }
}

Please note that you can launch Settings app via URL-scheme on iOS5 (versions lower than 5.0 (and greater than 5.0), don't support it). 请注意,您可以通过iOS5上的URL-scheme启动“设置”应用程序(版本低于5.0(且大于5.0),不支持)。 Call: 呼叫:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];

You can check if location services are enabled, before you try and update the location. 在尝试更新位置之前,您可以检查是否已启用位置服务。

Here is a snippet of the code I use: 这是我使用的代码片段:

if (TARGET_IPHONE_SIMULATOR) {
    currentLocation = [[CLLocation alloc] initWithLatitude:37.331718 longitude:-122.030629];
    [self methodNameHere:currentLocation];
} else {
    if ([CLLocationManager locationServicesEnabled]) {
        [locationManager startUpdatingLocation];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"Enable location services to do this" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

Basically, the code checks if you're targeting the simulator, which can give you errors if you try to get the location, so instead I just hard-code the location I want to simulate (in this case, Apple HQ). 基本上,代码会检查您是否针对模拟器,如果您尝试获取位置,这会给您带来错误,因此我只是硬编码我想要模拟的位置(在本例中为Apple HQ)。 If targeting the device, it checks if location services are enabled, if so, it calls the method you want to perform, if not, it shows the user an alert message. 如果定位设备,它会检查是否启用了位置服务,如果是,则调用您要执行的方法,否则会向用户显示警报消息。

I think a better way to approach this would be to do this: 我认为更好的方法是做到这一点:

switch ([CLLocationManager authorizationStatus]) {
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    case kCLAuthorizationStatusAuthorizedAlways:
        //we're good to go
        break;
    default:
        //pop up an alert
        break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM