【问题标题】:Xamarin.ios appropriate location to ask for permissionsXamarin.ios 请求权限的适当位置
【发布时间】:2020-05-08 08:39:10
【问题描述】:

我需要使用 Xamarin.Essentials 在 Xamarin.iOS 应用程序中请求权限。到目前为止,我发现的所有示例都是关于我没有使用的 Xamarin.Forms。

到目前为止,我发现的所有示例都是关于在按下按钮时询问权限,这对于实验来说很好,但对于生产应用程序却无用。

我有以下异步代码:

// TODO Move the permission requests to a more appropriate location?
var status = await Permissions.CheckStatusAsync<Permissions.Camera>();

if (status != PermissionStatus.Granted)
{
    status = await Permissions.RequestAsync<Permissions.Camera>();
}
status = await Permissions.CheckStatusAsync<Permissions.Microphone>();
{
    status = await Permissions.RequestAsync<Permissions.Microphone>();
}

我从 ViewDidLoad 异步覆盖中调用它。 每当我调用 Permissions.RequestAsync()

时,应用程序就会崩溃并显示以下输出
2020-05-08 10:21:05.815 yaka[714:200964] error: * Assertion at /Users/builder/jenkins/workspace/archive-mono/2020-02/ios/release/mono/mini/debugger-agent.c:4568, condition `array->len == 1' not met
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1014e7754: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x101065470: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x10123472c: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x101064370: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1014ab360: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1015ac108: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1015592a0: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x101559224: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x10105f0c0: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1014ab360: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1014e7754: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x101065470: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x10123472c: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x101064370: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1014ab360: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1015ac108: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1015592a0: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x101559224: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x10105f0c0: 8
[PLCrashReport] plframe_cursor_read_dwarf_unwind_int:131: Failed to find FDE the current frame pc: 0x1014ab360: 8

我的 info.plist 包含以下关于权限的内容:

<key>NSCameraUsageDescription</key>
<string>L&apos;accès est réservé au vidéoconférences.</string>
<key>NSMicrophoneUsageDescription</key>
<string>L&apos;accès est réservé au vidéoconférences.</string>

必须有适当的位置才能安全调用此编码,但我无法弄清楚。

任何帮助表示赞赏。

【问题讨论】:

  • 在 ViewDidLoad 中请求权限不是一个好的设计。您可以将它们移至方法 ViewDidAppear

标签: c# ios xamarin.ios xamarin.essentials


【解决方案1】:

您不需要针对 iOS 的特定代码。这就是 Essentials 的美妙之处。它利用了所谓的 bait & switch 模式。简而言之 - 每个平台的公开代码都是相同的,并且在编译时只会编译您需要的代码。可以看到权限码here

如果您查看Xamarin.Essentials.csproj,您会看到如果项目以iOS 为目标,则只会构建后缀为*.ios 的cs 文件。然后,在运行时,将调用适当的代码。

很可能,您在错误的地方调用它。尝试在 ViewDidAppear 中调用它,因为此错误听起来像是 Xamarin 期望某些内容已完全加载/可见,但事实并非如此。

总而言之 - 您可以使用可以在每个平台的官方文档页面上找到的代码示例,没有任何问题。只是要小心你打电话给他们的确切位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2018-12-21
    • 2017-09-13
    • 2016-12-06
    • 2015-12-07
    相关资源
    最近更新 更多