[英]How to determine if device location permission is denied twice in iOS swift?
如果在 iPhone 中禁用定位服務(設置 -> 隱私 -> 定位服務),並且當使用self.locationManager.requestWhenInUseAuthorization()
請求定位權限時,應用程序會顯示帶有設置和取消按鈕的系統對話框。
單擊設置按鈕后,它會重定向設備的位置服務頁面。
如果點擊取消,則警報消失。 如果我第二次請求許可並點擊取消,警報就會消失。 然后,如果我第三次請求位置許可,則不會顯示上述系統對話框。
我已經嘗試了CLLocationManager.locationServicesEnabled()
,這讓我啟用或禁用了設備位置。 在禁用 state 的情況下,我編寫self.locationManager.requestWhenInUseAuthorization()
,它只顯示系統對話框兩次。 現在如何確定系統對話被拒絕了兩次? 這樣我就可以顯示我的自定義對話框。
如果我理解得很好(你的問題有點混亂)你可以用 UserDefaults 那樣做,我使用 alert 但你可以在 if 語句中使用你的代碼:
導入 CoreLocation,設置 func 和 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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.