0
我正在研究一個按鈕,如果他授予應用程序位置權限,將會將用戶發送到地圖應用程序。問題是,如果我點擊一次按鈕並拒絕位置權限,下次我點擊該按鈕時,應用程序不會再次請求許可。似乎用戶「在授予地圖權限時只有一次鏡頭」。試圖在iOS中獲取位置權限Swift
下面是我用編程方式創建帶有回調函數的按鈕的代碼。還有一個「自定義iOS目標屬性」屏幕截圖,其中包含了隱私 - 位置使用說明,以便能夠使用用戶的位置。
directionsButton.addTarget(self, action: #selector(getDirection), for: UIControlEvents.touchUpInside)
func getDirection(sender: UIButton!) {
print("1")
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
let url = URL(string: "http://maps.apple.com/?daddr=60.79281049999999,10.688968899999963")
if (UIApplication.shared.canOpenURL(url!)) {
UIApplication.shared.openURL(url!)
} else {
print("Error")
}
print("2")
} else{
LocationManager.requestWhenInUseAuthorization()
print("3")
}
print("4")
}
/*
- Callback function for changes in location permissions
*/
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus){
print("change in auth")
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse){
let url = URL(string: "http://maps.apple.com/?daddr=60.79281049999999,10.688968899999963")
if (UIApplication.shared.canOpenURL(url!)) {
UIApplication.shared.openURL(url!)
} else {
print("Error")
}
} else{
self.view.makeToast("Couldn't get location permission", duration: 3.0, position: .bottom)
}
}
Picture of permissions added in the info-file