2017-09-25 125 views
12

從用戶請求位置的權限,當我使用的時候,我有一個問題iOS11我info.plist中包含位置權限問題11的iOS和iOS 10

<key>NSLocationWhenInUseUsageDescription</key> 
<string>When in use permissions</string> 
<key>NSLocationAlwaysUsageDescription</key> 
<string>always permissions</string> 
<key>NSLocationAlwaysAndWhenInUsageDescription</key> 
<string>Always and in usage permissions</string> 

我有兩個映射一個客戶和其他員工。對於員工而言,我需要知道使用

locationManager.requestAlwaysAuthorization()

對於客戶我只需要同時位置,即使應用程序沒有運行或者轉到後臺(簽約時,他們能夠將其關閉),它們的位置,並要求允許該應用程序正在使用中,並要求使用

locationManager.requestWhenInUseAuthorization()

在iOS系統11這只是請求允許在使用的時候,從來沒有在總的權限的權限。

在iOS 10中它具有正確的行爲。

我想要的行爲如下: 當他們是客戶(未登錄)時,它只會要求在使用時的權限。如果他們登錄(員工),即使在不使用時也請求位置。

如果任何人都可以闡明我錯過/做錯了什麼,將不勝感激。

需要注意的是,如果我刪除權限NSLocationAlwaysUsageDescription iOS10和iOS11有同樣的問題不總是請求權限。

稍微澄清一點。 我已經實現了didChangeAuthorization委託函數,並且當用戶允許來自調用requestWhenInUseAuthorization() 的警報的權限時被調用,但是當我在位置管理器上調用requestWhenInUseAuthorization()函數時,委託方法未被調用,就好像它從不接收該調用並且沒有提示對話框顯示給用戶。

+0

您是在真實設備上測試嗎?你是否在位置更新位置管理器委託方法 –

+0

是測試在兩個真實設備上測試iPhone 5運行iOS 10.3.3和iPhone 6運行iOS 11.我有位置管理器委託函數didChangeAuthorization和didUpdateLocations日誌。位置正在被調用(當在前臺),並且當我在請求使用權限時請求更改授權,但並不總是使用權限。 –

+0

看看這個答案https://stackoverflow.com/a/46339284/3024579 – Alok

回答

7

我想通過創建一個快速獨立的應用程序,只要求權限的問題,我被給了錯誤日誌th在聲明我使用的鑰匙是錯誤的。

我有NSLocationAlwaysAndWhenInUsageDescription而不是NSLocationAlwaysAndWhenInUseUsageDescription這是奇怪的,因爲從docs它聲明應該使用NSLocationAlwaysAndWhenInUsageDescription。切換到包括正確的關鍵固定問題和現在的權限如預期的iOS 11和10的作品。

感謝您的所有幫助。

0

對於這兩種情況,客戶和員工,你首先需要調用locationManager.requestWhenInUseAuthorization()

然後,只有當他們是員工,添加調用 locationManager.requestAlwaysAuthorization()

https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/request_always_authorization

概述要配置總是授權的位置服務,做 以下:添加NSLocationWhenInUseUsageDescription密鑰和 NSLocationAlwaysAndW henInUsageDescription鍵到Info.plist文件。 (Xcode中顯示這些鍵爲「隱私 - 位置在使用時用法 說明」和「隱私 - 位置始終和使用時使用 說明」中的Info.plist編輯)如果您的應用支持iOS的10 和更早版本,將NSLocationAlwaysUsageDescription密鑰添加到您的 Info.plist文件中。 (Xcode在Info.plist編輯器中將此項顯示爲「隱私 - 位置 始終使用說明」。)創建並配置您的CLLocationManager對象。請先調用 requestWhenInUseAuthorization()以啓用您的應用的基本 位置支持。當您使用需要授權級別的服務時,只調用requestAlwaysAuthorization()方法 。

+0

我應該使它更清楚時,應用程序打開每個人都被視爲一個客戶,它調用locationManager.requestWhenInUseAuthorization ()'''一旦他們登錄它然後請求'''locationManager.requestAlwaysAuthorization()'''我的理解是,只要我在某個時候使用請求時,我應該能夠始終跟蹤請求任何晚點。這是正確的還是我誤解了? –

+0

是的,這是正確的。 一些愚蠢的問題 - 1.您是否嘗試從iPhone中刪除應用程序並重新安裝?如果沒有,嘗試清理項目,刪除應用程序,然後重新安裝它。 2.檢查您的系統設置 - 用戶可以在系統設置中禁用位置服務,無論是針對您的應用程序還是針對所有應用程序。 – Roee84

+0

1)我確實嘗試安裝/卸載。 2)我檢查了我的系統設置,並且在iOS11上沒有始終的權限,不知道是否有幫助。 感謝您的幫助。 –

4

在你Info.plist文件添加此:

<key>NSLocationUsageDescription</key> 
<string></string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string></string> 
在迅速文件

現在,不要忘了添加委託:CLLocationManagerDelegate

在您的viewDidLoad(),補充一點:

locationManager = CLLocationManager() 

locationManager.delegate = self 

locationManager.requestWhenInUseAuthorization() 

locationManager.desiredAccuracy = kCLLocationAccuracyBest 

locationManager.startUpdatingLocation() 

locationManager.startMonitoringSignificantLocationChanges() 

// Here you can check whether you have allowed the permission or not. 

if CLLocationManager.locationServicesEnabled() 
    { 
     switch(CLLocationManager.authorizationStatus()) 
     { 

     case .authorizedAlways, .authorizedWhenInUse: 

      print("Authorize.") 

      break 

     case .notDetermined: 

      print("Not determined.") 

      break 

     case .restricted: 

      print("Restricted.") 

      break 

     case .denied: 

      print("Denied.") 
     } 
    } 
+3

不幸的是,這不是它需要NSLocationAlwaysAndWhenInUsageDescription https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/request_always_authorization爲iOS 11問題 –

+2

你不需要休息的快速切換的情況下AKHIL – Cesare

+0

@Cesare我的背景是在C#和Java中。感謝您指出。大聲笑 :) –