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?
Asked
Active
Viewed 1,926 times
3 Answers
6
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) {
}

Jaydeep Vora
- 6,085
- 1
- 22
- 40
0
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

Jaydeep Vyas
- 4,411
- 1
- 18
- 44

Nirav Kotecha
- 2,493
- 1
- 12
- 26
0
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()
}
}

Raksha Saini
- 604
- 12
- 28