【问题标题】:App crash when denied location permission in Xamarin iOS在 Xamarin iOS 中拒绝位置权限时应用程序崩溃
【发布时间】:2019-06-25 00:22:38
【问题描述】:

我需要在我的应用中设置位置,当用户通过单击“不允许”拒绝位置权限时,应用会崩溃。我得到了“System.Reflection.TargetInvocationException”。但是一旦我再次重新开始,即使在那个时候权限被拒绝它也不会崩溃。在android中它工作正常。我需要更新权限插件或更改info.Plist吗? 这是我的代码

          var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
            if (status != PermissionStatus.Granted)
            {
                if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
                {
                    await Application.Current.MainPage.DisplayAlert("Location denied", "app needs access to location for this operation.", "OK");
                }

                var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
                status = results[Permission.Location];
            }
            if (status == PermissionStatus.Granted)
            {
                  // do something
            }
            else if (status != PermissionStatus.Unknown)
            {
                  // Crash when user denied permission 
            }

【问题讨论】:

  • 也分享崩溃日志。
  • @RoHaN 没有日志它只是因为显示 Main.Cs 文件而崩溃
  • @RoHaN 未处理异常:System.Reflection.TargetInvocationException:调用目标已引发异常。线程完成: #4 线程 0x4 以代码 0 (0x0) 退出。
  • 用 try/catch 块包围你的代码。您可能会获得有关此异常的更多信息。
  • 同时检查内部异常。像这样:===============try { // code block causing TargetInvocationException } catch (Exception e) { if (e.InnerException != null) { string msg = e.InnerException.Message; } } ===============

标签: ios xamarin xamarin.forms permissions location


【解决方案1】:

您应该在 Info.plist 中添加项目

参考链接:App pop Up saying "... Would like to access your (Camera, location, photos, etc)"

和文档:Apple Document

【讨论】:

  • 我已经在我的 info.Plist 中授予了位置权限
【解决方案2】:

检查您的 MainActivity OnRequestPermissionsResult()。逐一评论权限的接收者并运行。你会找到罪魁祸首。祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2023-01-27
    相关资源
    最近更新 更多