Reputation: 41
If Location Service is disabled in iPhone(Settings -> Privacy -> Location Services) and when requested for location permission using self.locationManager.requestWhenInUseAuthorization()
then app shows system dialog with settings and cancel button.
On click of settings button it redirects Device's location service page.
If Cancel is tapped alert dismisses. And if second time I ask for permission and tap cancel, alert dismisses. And then again if I request third time for location permission then that above system dialog won't be shown.
I have tried CLLocationManager.locationServicesEnabled()
which gives me Device location is enabled or disabled. In case of disable state I have written self.locationManager.requestWhenInUseAuthorization()
which shows system dialog twice only. Now how shall I determine that system dialog has been denied twice? So that I can show my custom dialog.
Upvotes: 4
Views: 1676
Reputation: 5648
if I understand well (your question is a little bit confused) you can do it with UserDefaults like that, I use alert but you can use your code in if statement:
import CoreLocation, set func and UserDefaults:
import UIKit
import CoreLocation
class Prova: UIViewController {
let locationManager = CLLocationManager() // location manager
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
locationManagerDidChangeAuthorization(locationManager)
}
var control = 0 // set var to control user tap denied
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedAlways , .authorizedWhenInUse:
print("authorized always and when in use")
break
case .notDetermined , .denied , .restricted:
print("authorized denied or restricted")
let res = UserDefaults.standard.integer(forKey: "ok")
control = res
UserDefaults.standard.set(res, forKey: "ok")
if res >= 1 { // first = 0 second = 1 - Two times
DispatchQueue.main.async {
let myAlert = UIAlertController(title: "Ooooooop!", message: "you're denied or restricted for two times", preferredStyle: .alert)
myAlert.addAction(UIAlertAction(title: "Cancel", style: .destructive))
UserDefaults.standard.set(self.control + 1, forKey: "ok")
self.present(myAlert, animated: true)
}
} else {
DispatchQueue.main.async {
let myAlert = UIAlertController(title: "OK", message: "you're denied for: \(self.control + 1) times", preferredStyle: .alert)
myAlert.addAction(UIAlertAction(title: "Cancel", style: .destructive))
UserDefaults.standard.set(self.control + 1, forKey: "ok")
self.present(myAlert, animated: true)
}
}
break
default:
break
}
print(" time denied:",control)
}
}
Upvotes: 2