在Xamarin iOS中拒绝位置权限时,应用崩溃

时间:2019-01-31 14:03:16

标签: ios xamarin xamarin.forms permissions location

我需要在我的应用中找到位置,并且当用户通过单击“不允许”拒绝位置许可时,应用崩溃。我得到了“ 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 
            }

2 个答案:

答案 0 :(得分:1)

您应在Info.plist中添加项目

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

和文档:Apple Document

答案 1 :(得分:0)

Check your MainActivity OnRequestPermissionsResult(). Comment one by one another receivers of permissions and run. You will find the culprit. Good luck!