Reputation: 4313
When user is on MainViewController
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
}
viewDidLoad()
will run and the alert will appear
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Accessing user location to determine the nearest pickup location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Accessing user location to determine the nearest pickup location</string>
The main question is, if user clicks Don't allow, how do I check programmtically? for example
// Example code snippet, not from apple officials
if NSlocation is nil then I want to do some alert
Upvotes: 1
Views: 535
Reputation: 3499
If the user chooses to deny your app access to location services, CLLocationManager.authorizationStatus()
will be .denied
(though you should also watch out for .restricted
. If you'd like to be notified when the user chooses whether or not to allow or deny your app, you can make yourself your location manager's delegate
and implement the proper method:
class MainViewController: CLLocationManagerDelegate{
let locationManager = CLLocationManager()
override func viewDidLoad() -> Void{
super.viewDidLoad()
locationManager.delegate = self
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) -> Void{
switch status{
case .authorizedAlways, .authorizedWhenInUse:
// You're allowed to use location services
case .denied, .restricted:
// You can't use location services
default:
// This case shouldn't be hit, but is necessary to prevent a compiler error
}
}
}
Upvotes: 3
Reputation: 6611
Use below delegate method of CLLocationManager
to check user click on Don't allow button in permission alert.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .denied:
print("Show you own alert here")
break
}
}
To redirect user in location settings follow below:
Write this code on button action:
UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=LOCATION_SERVICES")!)
Upvotes: 0