本文介紹了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.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答