问题
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?
回答1:
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
回答2:
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");
}
}
回答3:
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.
来源:https://stackoverflow.com/questions/16075337/get-alert-for-current-location-every-time