Skip to content

Commit

Permalink
Also ask for access to current location if previously denied or restr…
Browse files Browse the repository at this point in the history
…icted
  • Loading branch information
nighthawk committed Mar 13, 2023
1 parent 9a31df1 commit 440c3de
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions Sources/TGCardViewController/TGCompatibleMapBuilder+MapKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,21 @@ public class TGMapKitBuilder: TGCompatibleMapBuilder {
tracker.centerXAnchor.constraint(equalTo: background.centerXAnchor).isActive = true
tracker.centerYAnchor.constraint(equalTo: background.centerYAnchor).isActive = true

// MKUserTrackingButton in iOS 11 just goes into a dumb spinner mode, if
// no permissions are granted. To work around this, we disable it and instead
// intercept taps.
// The way we handle the DisposeBag might(?) introduce a retain cycle, but
// this only exists until you provide access to the current location.
if CLLocationManager.authorizationStatus() == .notDetermined {
// MKUserTrackingButton in iOS 11-16 just goes into a dumb spinner mode, if
// no permissions are granted. To work around this, we disable it and
// instead intercept taps.
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .denied, .restricted:
Self.updateTracker(tracker, enabled: false)
let tapper = UITapGestureRecognizer()
tapper.addTarget(self, action: #selector(trackerButtonPressed))
background.addGestureRecognizer(tapper)

case .authorizedAlways, .authorizedWhenInUse:
break

@unknown default:
assertionFailure()
}

if #available(iOS 13.4, *) {
Expand All @@ -111,15 +116,21 @@ public class TGMapKitBuilder: TGCompatibleMapBuilder {
preconditionFailure()
}

// authorisation might have since been granted
guard CLLocationManager.authorizationStatus() == .notDetermined else {
switch CLLocationManager.authorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse:
// authorisation might have since been granted
Self.updateTracker(tracker, enabled: true)
tracker.mapView?.userTrackingMode = .follow
return
}

askForLocationPermissions? { success in
guard success else { return }

case .notDetermined, .denied, .restricted:
askForLocationPermissions? { success in
guard success else { return }
Self.updateTracker(tracker, enabled: true)
tracker.mapView?.userTrackingMode = .follow
}

@unknown default:
assertionFailure()
Self.updateTracker(tracker, enabled: true)
tracker.mapView?.userTrackingMode = .follow
}
Expand Down

0 comments on commit 440c3de

Please sign in to comment.