如何在iOS上启动应用程序时请求权限?

时间:2019-06-23 19:46:11

标签: ios xamarin.forms

enter image description here如何在启动应用程序时在Ios上请求权限? 警报窗口不会出现。

对Info.plist文件进行了以下设置:

<key>UIBackgroundModes</key>
 <array>
    <string>location</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>Required for the application to work</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Required for the application to work</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Required for the application to work</string>

警告窗口不出现。

1 个答案:

答案 0 :(得分:1)

使用Location时,仅在Info.plist中添加键值是不够的,还需要在应用运行时请求权限。

添加以下代码进行尝试:

CLLocationManager locationManager = new CLLocationManager();

locationManager.RequestWhenInUseAuthorization();
//Requests permission to use location services while the app is in the foreground.

Or

locationManager.RequestAlwaysAuthorization();
//Requests permission to use location services whenever the app is running.

这里是apple documentxamarin sample.