简体   繁体   中英

Prompt for general iOS Location Services

I would like to ask the user to enable the general Locations Services (not the app specific Location Services) if as a whole they never get prompted to enable it for the app.

Is there a way to prompt the user to turn on Location Services as a whole every time they open the app and it is disabled?

Code found in other threads, but it is App specific, not general Location Services:

if(![CLLocationManager locationServicesEnabled]) {
    NSLog(@"Location Services is OFF GLOBALLY");

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString( @"Location Services", @"" ) message:NSLocalizedString( @"Please Set Location Services to On and Restart App\n\nSettings->Privacy->Location Services", @"" ) preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"OK", @"" ) style:UIAlertActionStyleDefault handler:nil];

    [alertController addAction:okAction];

    [self presentViewController:alertController animated:YES completion:nil];


}
else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
    NSLog(@"Location Services is Never for App");

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString( @"App Needs Location Services", @"" ) message:NSLocalizedString( @"Please Set Location Services to Always for App", @"" ) preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"Settings", @"" ) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];  

    [alertController addAction:settingsAction];

    [self presentViewController:alertController animated:YES completion:nil];

}

In your info.plist file, you'll want to include one of the following lines:

If it's always in use, then add this entry to info.plist :

NSLocationAlwaysUsageDescription

The string associated with that entry should be the message you want displayed: `Would you like to enable continual location monitoring even when this app is not in use?"

...or...

You could do NSLocationWhenInUseUsageDescription and a message along the lines of, "Would you like to enable location monitoring only when this app is in use?"

Here's documentation covering this topic

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