Fist time when I install my iOS app on a device via xcode and location service is off, requestAlwaysAuthorization() shows "Turn on location services" that push user to device Settings to turn on location service, then user comes back to the app and would see "permission" alert(with three options always, while using and never). If user taps on always option then closes the app completely and turns location service off then opens app again, "Turn on location services" alert is not displayed. This is my code:
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
NotificationCenter.default.addObserver(self, selector: #selector(checkLocationService), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
}
@objc func checkLocationService() {
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .denied, .notDetermined, .restricted:
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
case .authorizedWhenInUse, .authorizedAlways:
...
}
} else {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
}
I've added all three location keys in the Info.plist:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>My app need your location to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>My app need your location to work</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>My app need your location to work</string>
I am testing on iOS 11 and 12 and I have no idea what is wrong.