get alert for current location every time

2019-09-17 04:06发布

I am trying to get device current location using CLLocationManage. I set my method to get location on button click. I can get location successfully but when I get alertview with message "APPNAME would like to use your current location?" with two buttons, "Dont Allow" and "Ok". and I click on "Dont Allow". Then whenever I click on button and I cant get that alertview again to get current location, so I am not able to get the location. So is it possible to get alertview everytime when I click on my button to get location?

3条回答
\"骚年 ilove
2楼-- · 2019-09-17 04:40

When you click on "don't allow" button on alert, Location permission for your application is restricted.

You can navigate to settings in your phone > privacy >location services, over there you can see services are off for you application. You can turn it on to give permissions

查看更多
甜甜的少女心
3楼-- · 2019-09-17 04:57

The selected answer is correct, for the user.

However, if you're a programmer and you want user to be notified with the alert, you should call

[singleton.locationManager startUpdatingLocation];

This will automatically pops the alert when location services are disabled every time I think as you wish.

A common mistake is to check whether location update is enabled and don't bother calling startUpdatingLocation when it's not.

If that's the case, then the alert will not shows up.

查看更多
The star\"
4楼-- · 2019-09-17 05:04

Adding this answer so you can more efficiently handle that scenario.

There is no way to force current location permission dialog again, but what you can do is trap its status that user has denied use of location in your app using CLLocationManagerDelegate,

- (void)locationManager:(CLLocationManager*)aManager didFailWithError:(NSError*)error
{
    switch([error code])
    {
    case kCLErrorDenied: // Location access denied
    NSLog(@"Sorry, this app needs to access your current location... ");
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Sorry, this 
    app needs to access your current location. You can navigate to settings in your 
    phone > privacy >location services, over there you can see services are off for you 
    application. You can turn it on to give permissions"  
    delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    //show the alert view
    [myAlert show];
    break;

    case kCLErrorNetwork: // received n/w error
    NSLog(@"Network error occurred");
    }
}
查看更多
登录 后发表回答