【发布时间】: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