0

I have a switch for the user to disable / enable the use of the location by the application. I am having two problems.

1 - When the native ios popup appears to ask if he wants to allow the use of location, and he says no, the promixa time I request permission popup is no longer displayed, and the only way to enable the user permission It is in the iPhone settings.

2 - If the user has allowed the use of the location, but then at some point you want to disable the switch is present in the application, it can not.

below is the code I am using.

-(IBAction)avancar:(id)sender{

    if (locationManager == nil) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
    }


    if (switchPermissao.isOn) {
        [locationManager startUpdatingLocation];

        if(IS_OS_8_OR_LATER) {
            [locationManager requestWhenInUseAuthorization];
            [locationManager requestAlwaysAuthorization];
        }

    }else{
        [locationManager stopUpdatingLocation];
        [self performSegueWithIdentifier:@"tela2" sender:self];
    }
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status == kCLAuthorizationStatusDenied) {
        [switchPermissao setOn:NO animated:YES];

    }
    else if (status == kCLAuthorizationStatusAuthorizedAlways
             || status == kCLAuthorizationStatusAuthorizedWhenInUse) {

        [switchPermissao setOn:YES animated:YES];
    }

 [self performSegueWithIdentifier:@"tela2" sender:self];    

}
StefanS
  • 1,089
  • 1
  • 11
  • 38
jucajl
  • 1,195
  • 3
  • 12
  • 29
  • 1
    Re 1: you can't avoid this, but it is possible to open the Settings app from your own app: see [here](http://stackoverflow.com/questions/5655674/opening-the-settings-app-from-another-app). – Glorfindel Jul 31 '15 at 13:52
  • You can't display the popup repeatedly. Your application needs to (as gracefully as possible) handle the fact that you don't have location data. If it's an absolute requirement, you can ensure your view/controller displays a button or similar alert suggesting they go to Settings and turn it back on. – Craig Otis Jul 31 '15 at 14:39

1 Answers1

0

RE: 1) The OS will only show the permission dialog once. After the user makes their selection, the OS will not show the permission dialog again even if your code asks for permission again. So, you must handle this with your code to present the user with a custom dialog/alert. To do this, use the CLLocationManager authorizationStatus to get the current status. Only when this status is kCLAuthorizationStatusNotDetermined will the OS show the system permission dialog. In all other cases you need to handle the permission with a custom dialog.

RE: 2) Your app cannot change the status of location services, this can only be changed by the user under the system Setting. To handle this you can present a custom dialog that will open the system Settings for your app so the user can change the status. You can use UIApplicationOpenSettingsURLString, in iOS 8, to open the system Settings for your app.

rmp
  • 3,503
  • 1
  • 17
  • 26