iOS11中的定位授权

前台定位权限

1,增加NSLocationWhenInUseUsageDescription。
2,创建CLLocationManager对象,在使用定位服务前调用requestWhenInUseAuthorization()。
3,通过func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)处理权限变化。该函数并不会在每次改改变权限后都会被调用。

参考:
1,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)处理权限变化。该函数并不会在每次改改变权限后都会被调用。
参考:
1,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.

猜你喜欢

转载自blog.csdn.net/dangyalingengjia/article/details/77965029