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 {
showAcessDeniedAlert()
}
manager.onLocationFix = {
}
}
}
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
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()
}
}
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"?
source
share