简体   繁体   中英

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. 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. 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). 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). 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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