Skip to content

Commit e3ca437

Browse files
committed
Fix location permissions warning issue
Fixed an issue where a location permissions warning alert controller was presented in all cases for a fresh install of the app. The problem was that the app did not check if the location permissions were requested before, so even if the user was never presented with a prompt the warning case would trigger - meaning users would always be shown the permissions denied warning no matter what they chose. The fix is to rely on the `CLLocationManagerDelegate` call back of `locationManagerDidChangeAuthorization(_ :)` which is called both at initialization time of `CLLocationManager` and when permissions are changed. The check at `appDidBecomeActive` time has been removed because the aforementioned delegate call accomplishes the same thing.
1 parent 32fe5df commit e3ca437

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

OpenGpxTracker-Watch Extension/InterfaceController.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,25 @@ class InterfaceController: WKInterfaceController {
331331
/// - Seealso: displayLocationServicesDisabledAlert, displayLocationServicesDeniedAlert
332332
///
333333
func checkLocationServicesStatus() {
334-
//Are location services enabled?
335-
if !CLLocationManager.locationServicesEnabled() {
336-
displayLocationServicesDisabledAlert()
334+
let authorizationStatus = CLLocationManager.authorizationStatus()
335+
336+
//Has the user already made a permission choice?
337+
guard authorizationStatus != .notDetermined else {
338+
//We should take no action until the user has made a choice
337339
return
338340
}
341+
339342
//Does the app have permissions to use the location servies?
340-
if !([.authorizedAlways, .authorizedWhenInUse].contains(CLLocationManager.authorizationStatus())) {
343+
guard [.authorizedAlways, .authorizedWhenInUse ].contains(authorizationStatus) else {
341344
displayLocationServicesDeniedAlert()
342345
return
343346
}
347+
348+
//Are location services enabled?
349+
guard CLLocationManager.locationServicesEnabled() else {
350+
displayLocationServicesDisabledAlert()
351+
return
352+
}
344353
}
345354

346355
///

OpenGpxTracker/ViewController.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
873873
if !wasSentToBackground {
874874
return
875875
}
876-
checkLocationServicesStatus()
877876
locationManager.startUpdatingLocation()
878877
locationManager.startUpdatingHeading()
879878
}
@@ -1164,16 +1163,26 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
11641163
/// - Seealso: displayLocationServicesDisabledAlert, displayLocationServicesDeniedAlert
11651164
///
11661165
func checkLocationServicesStatus() {
1167-
//Are location services enabled?
1168-
if !CLLocationManager.locationServicesEnabled() {
1169-
displayLocationServicesDisabledAlert()
1166+
let authorizationStatus = CLLocationManager.authorizationStatus()
1167+
1168+
//Has the user already made a permission choice?
1169+
guard authorizationStatus != .notDetermined else {
1170+
//We should take no action until the user has made a choice
1171+
//Note that we request location permission as part of the property `locationManager` init
11701172
return
11711173
}
1174+
11721175
//Does the app have permissions to use the location servies?
1173-
if !([.authorizedAlways, .authorizedWhenInUse].contains(CLLocationManager.authorizationStatus())) {
1176+
guard [.authorizedAlways, .authorizedWhenInUse ].contains(authorizationStatus) else {
11741177
displayLocationServicesDeniedAlert()
11751178
return
11761179
}
1180+
1181+
//Are location services enabled?
1182+
guard CLLocationManager.locationServicesEnabled() else {
1183+
displayLocationServicesDisabledAlert()
1184+
return
1185+
}
11771186
}
11781187
///
11791188
/// Displays an alert that informs the user that location services are disabled.
@@ -1288,8 +1297,6 @@ extension ViewController: PreferencesTableViewControllerDelegate {
12881297
signalAccuracyLabel.text = kUnknownAccuracyText
12891298
}}
12901299

1291-
// MARK: location manager Delegate
1292-
12931300
/// Extends `ViewController`` to support `GPXFilesTableViewControllerDelegate` function
12941301
/// that loads into the map a the file selected by the user.
12951302
extension ViewController: GPXFilesTableViewControllerDelegate {
@@ -1412,6 +1419,15 @@ extension ViewController: CLLocationManagerDelegate {
14121419
map.updateHeading() // updates heading view's rotation
14131420

14141421
}
1422+
1423+
///
1424+
/// Called by the system when `CLLocationManager` is created and when the user makes a permission choice
1425+
///
1426+
/// We handle this delegate callback so that we can check if the user has allowed location access, else we show a warning
1427+
///
1428+
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
1429+
checkLocationServicesStatus()
1430+
}
14151431
}
14161432

14171433
extension Notification.Name {

0 commit comments

Comments
 (0)