iOS11中的定位授权的解决方法

2019/7/9 22:42:27

本文主要是介绍iOS11中的定位授权的解决方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文介绍了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中的定位授权的解决方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程