Search code examples
objective-ciphoneios8mapkitcllocationmanager

iphone location service alert call back allow and don't allow handling


I have a button in which on click I am authorizing my app to enable location services. I want to capture the allow button event from the alert. As the user press allow, my button color will change to some other color. I am unable to capture the callback of allow button. However I am able to capture don't allow button event with help of this link

Location service iOS alert call back

Please can anyone tell me how to capture the allow button tap and on success I want to do my code.


Solution

  • Implement this:

    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
        if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
            //allowed - do your logic
        }
        else if (status == kCLAuthorizationStatusDenied) {
            //denied
        }
    }
    

    Swift 4/5:

    self.locationManager.requestAlwaysAuthorization()
    

    Then handle in delegate method:

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .notDetermined:
            manager.requestAlwaysAuthorization()
        case .restricted, .denied:
            // location services unauthorized
        case .authorizedAlways, .authorizedWhenInUse:
            manager.startUpdatingLocation()
        @unknown default:
            print("New CLLocationManager.authorizationStatus() not handled!")
        }    
    }
    

    Remember to set locationManager delegate:

    self.locationManager.delegate = self;
    

    And implement CLLocationManagerDelegate by your class.