2017-02-10 68 views
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

回答

3

好像用戶具有 「在批准的地圖的權限只一擊」。

是的,這是位置權限的情況,因爲它是用於其他事情,如地址簿訪問權限。 您的應用程序可以檢測到用戶以前拒絕了許可,如果是這樣,告訴他們需要通過設置使用名爲「帶我到設置」之類的按鈕啓用它。 點擊按鈕後,您可以通過以下方式啓動應用程序的設置:

UIApplication.shared.open(appSettings as URL, options: [:], completionHandler: { (results) in 
    ... 
    })