user6120934
user6120934

Reputation:

How to detect when user pressed the "allow" when user location is requested

I am creating an app which uses the user's current location. At first run, after the user is prompted to allow or deny the app to use it's location, I want to do stuff when the user pressed the "allow" button. Is there a way to detect when the user has pressed the "allow" button?

Upvotes: 3

Views: 2339

Answers (3)

Raksha Saini
Raksha Saini

Reputation: 603

For Swift 3X

you can click authorizedalways allow and deny then call this method and get some information about this.

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.NotDetermined {
    locationManager.requestWhenInUseAuthorization()
}
}

Upvotes: 0

Nirav Kotecha
Nirav Kotecha

Reputation: 2581

You don't have direct access to that alert.

If the user click on "Don't Allow" then CLLocationManager will call locationManager:didFailWithError: on its delegate. The error domain will be kCLErrorDomain and the error code will be kCLErrorDenied.

you can check the status whethter user allowed or not.

let notificationType = UIApplication.shared.currentUserNotificationSettings?.types
        if notificationType?.rawValue == 0 {
            self.openPushNotificationDialog() //user denied or don't allowed
        } 

Official link for notification https://developer.apple.com/documentation/usernotifications/unauthorizationstatus

Upvotes: 0

Jaydeep Vora
Jaydeep Vora

Reputation: 6213

Swift 3.0

You can use below delegate method to detect the user is authorized location or not. If user allowed the location, then it will returns status .authorizedAlways or .authorizedWhenInUse.

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

}

Upvotes: 6

Related Questions