IOS Swift 3: Location Privacy Denied

What should I do if the option "Access privacy for the application" is denied?

Evening guys, I am coding a simple application that uses location when used .

Design Pattern

  • First of all, when you run the application, it will check if there is already a installed permission.

    • If not, a warning message is displayed asking for permission.
    • If yes, and granted, he continues to do his job.
    • If β€œyes” and β€œdenied”, a warning is displayed asking for access using a button indicating settings.
      • It should be possible to return from the settings to the application.
  • Applications do their job.

  • If the user changes the privacy settings while the application is still open, the application should be notified and repeat number 1.

Code still

Maincontroller

let manager = LocationManager()

override func viewDidLoad() {
        super.viewDidLoad()
        // ...

        if manager.getPermission() == false {
          //show alert
          showAcessDeniedAlert()
        }
        manager.onLocationFix = { 
          //This is a function used for a closure
        }

    }
}
func showAcessDeniedAlert() {
    let alertController = UIAlertController(title: "Location Accees Requested",
                                                message: "The location permission was not authorized. Please enable it in Settings to continue.",
                                                preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (alertAction) in

        // THIS IS WHERE THE MAGIC HAPPENS!!!!
        if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
            UIApplication.shared.open(appSettings as URL)
        }
    }
    alertController.addAction(settingsAction)

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)
}

Locationmanager

import CoreLocation

extension Coordinate {
    init(location: CLLocation) {
        latitude = location.coordinate.latitude
        longitude = location.coordinate.longitude
    }
}

final class LocationManager: NSObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()

    var onLocationFix: ((Coordinate) -> Void)?

    override init() {
        super.init()
        manager.delegate = self
        manager.requestLocation()
    }

  func getPermission() -> Bool {
      switch CLLocationManager.authorizationStatus() {
      case .authorizedAlways:
            return true
      case .authorizedWhenInUse:
            return true
      case .denied:
            return false
      case .restricted:
            return false
      case .notDetermined:
            manager.requestWhenInUseAuthorization()
            return getPermission()
      }
  }



    //MARK: CLLocationManagerDelegate
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            manager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else { return }

        let coordinate = Coordinate(location: location)
        if let onLocationFix = onLocationFix {
            onLocationFix(coordinate)
        }

    }
}

How can I?

  • How can I show AlertController if privacy is disabled?

    With this setup I have this error: Warning: Attempt to present <UIAlertController: 0x145ae7ee0> on <xxx.xxController: 0x143e04720> whose view is not in the window hierarchy!.

  • How can I encode a settings button pointing to Settings?

  • How can I encode: "from the settings page, I could return to the application"?

+4
source share
1 answer
  • viewDidLoad self.view. , first viewDidLoad self.view . viewDidAppear(_:) , .
  • , , , , .
  • - , .
+2

Source: https://habr.com/ru/post/1658305/


All Articles