這篇文章主要介紹了如何解決iOS11中定位授權的問題,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
前臺定位權限
1,增加NSLocationWhenInUseUsageDescription。
2,創建CLLocationManager對象,在使用定位服務前調用requestWhenInUseAuthorization()。
3,通過func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)處理權限變化。該函數并不會在每次改改變權限后都會被調用。
參考:
https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/requesting_when_in_use_authorization
前后臺定位權限
如果希望在app在前臺后臺都可以使用定位服務,需要完成以下幾點:
1,在plist中增加NSLocationWhenInUseUsageDescription和NSLocationAlwaysAndWhenInUsageDescription,如果需要支持iOS10的話,增加NSLocationAlwaysUsageDescription。
2,創建CLLocationManager對象,用requestWhenInUseAuthorization()獲得基本定位支持,用requestAlwaysAuthorization()獲得前后臺定位支持。
3,在第一次調用requestAlwaysAuthorization()時,會提示NSLocationAlwaysAndWhenInUsageDescription中指定的文字,讓用戶選擇是升級到全權限還是,前臺權限。
4,通過func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)處理權限變化。該函數并不會在每次改改變權限后都會被調用。
參考:
https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/request_always_authorization
注意:
requestWhenInUseAuthorization只有在用戶未選擇時會彈出對話框。
* When +authorizationStatus != kCLAuthorizationStatusNotDetermined, (ie
* generally after the first call) this method will do nothing.
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何解決iOS11中定位授權的問題”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。