【问题标题】:How on ios when launching an application, request permission?在ios上启动应用程序时如何请求权限?
【发布时间】:2019-06-23 19:46:11
【问题描述】:

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>

警报窗口不出现。

【问题讨论】:

    标签: ios xamarin.forms


    【解决方案1】:

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

    添加以下代码试一试:

    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.

    【讨论】:

    • 在 ...\Views\myfile.cs 添加代码时,我收到一条警告 - 无法找到属性或命名空间 CLLocationManager 添加后提供添加 using CoreLocation;。我收集应用程序并收到错误Error processing method: 'System.Void System.Net.Http.NSUrlSessionHandler / NSUrlSessionDataTaskStream / &lt;ReadAsync&gt; d__14 :: MoveNext ()' in assembly: 'Xamarin.iOS.dll' ---&gt; System.ArgumentNullException: Value cannot be uncertain.
    • @alexandrf Okey,代码需要在 IOS(ViewController.cs 或 AppDelegate.cs)中运行,而不是在 Forms 中。您可以参考上面的示例查看。
    • 需要加using CoreLocation吗?在示例“xamarin”中,我没有找到using CoreLocation。我的项目在添加代码CLLocationManager locationManager = new CLLocationManager(); locationManager.RequestWhenInUseAuthorization();时需要using CoreLocation
    • @alexandrf 不,只要加上using Foundation; 然后CLLocationManager 就可以了。
    • @alexandrf Eh,Core Location是一个Framework,可以直接在Xamrin.IOS中使用。其实和其他Using xx无关。
    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多