【问题标题】:Crash on exporting file via UIActivityViewController通过 UIActivityViewController 导出文件时崩溃
【发布时间】:2020-03-06 19:55:49
【问题描述】:

以下代码使用UIActivityViewController将我应用的“tmp”文件夹中的文件导出到其他应用。

private func exportFile(_ fileURL: URL) {
    let items: [Any] = [fileURL]

    let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)

    present(activityViewController, animated: true, completion: nil)
}

这段代码运行良好,我可以导出到iCloudGoogle DriveMailOutlookiZip(后者使用“复制到 iZip”)。

但是,我的应用程序最近在导出时选择'Copy to iZip' 后开始崩溃,但导出到其他应用程序工作正常。在它正常工作和崩溃之间,iOS、iZip 和我的应用程序都有更新。

Crashlytics 报告以下内容,但这在 iOS 中看起来很深,很难知道问题的根本原因是什么:

致命异常:NSInvalidArgumentException *** -[__NSArrayM insertObject:atIndex:]: 对象不能为 nil

ShareSheet __79-[UIActivityViewController willPerformInServiceActivityWithRequest:completion:]_block_invoke_3

这发生在运行 iOS 13.3.1 的 iPhone 上

关于 NSInvalidArgumentException 中断的调试器报告:

> (lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000189ffdf6c libobjc.A.dylib`objc_exception_throw
    frame #1: 0x000000018a32c360 CoreFoundation`_CFThrowFormattedException + 112
    frame #2: 0x000000018a32b9f4 CoreFoundation`-[__NSArrayM insertObject:atIndex:].cold.1 + 48
    frame #3: 0x000000018a1ae120 CoreFoundation`-[__NSArrayM insertObject:atIndex:] + 1104
    frame #4: 0x0000000195914e80 ShareSheet`__79-[UIActivityViewController willPerformInServiceActivityWithRequest:completion:]_block_invoke_3 + 1512
    frame #5: 0x00000001089497fc libdispatch.dylib`_dispatch_call_block_and_release + 24
    frame #6: 0x000000010894abd8 libdispatch.dylib`_dispatch_client_callout + 16
    frame #7: 0x0000000108958c34 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1316
    frame #8: 0x000000018a2545e4 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
    frame #9: 0x000000018a24f5d8 CoreFoundation`__CFRunLoopRun + 2004
    frame #10: 0x000000018a24eadc CoreFoundation`CFRunLoopRunSpecific + 464
    frame #11: 0x00000001941ef328 GraphicsServices`GSEventRunModal + 104
    frame #12: 0x000000018e35c63c UIKitCore`UIApplicationMain + 1936
  * frame #13: 0x00000001024f1078 xApp`main at AppDelegate.swift:41:7
    frame #14: 0x000000018a0d8360 libdyld.dylib`start + 4
(lldb) 

第 4 帧断点处的代码 (0x195914e80):

> WTFLogLevel, WTF::Logger::LogSiteIdentifier const&, char const (&) [7], unsigned long const&) (.cold.1)   
0x195914e80 <+1512>: mov    x28, x23
0x195914e84 <+1516>: ldr    x0, [x23, #0xba0]
0x195914e88 <+1520>: ldr    x1, [sp, #0x38]
0x195914e8c <+1524>: mov    x2, x25
0x195914e90 <+1528>: mov    x3, x20
0x195914e94 <+1532>: bl     0x193904be4               ; symbol stub 

知道问题可能是什么吗?我不确定这是否是我的应用、iOS 或 iZip 的问题。

【问题讨论】:

  • 看起来您正试图将 nil 的内容插入到数组中。那么根据你分享的代码,items 是什么样子的,可能是nil 吗?
  • @koen,谢谢,但 items 是一个包含文件 URL 的数组,不是零。似乎稍后在 iOS 深处的某个地方尝试将 nil 写入另一个数组,但这仅在通过“复制到 iZip”共享文件时发生,而不是在共享到提到的其他应用程序时发生。
  • 您可以发布包含违规行的代码吗?你可以在 Xcode 中添加一个异常断点,它会告诉你它在哪里崩溃。 stackoverflow.com/questions/17802662/…
  • 我添加了@koen 建议的异常断点信息。给人的印象是它在 WebKit 函数中崩溃了 - github.com/WebKit/webkit/blob/master/Source/WTF/wtf/Logger.h
  • 你联系过 iZip 开发者了吗?

标签: ios swift uiactivityviewcontroller


【解决方案1】:

感谢 @koen 的建议,我联系了 iZip 支持,并得到了非常快速和有用的回复。这也与@WILL K. 的尝试不同目录的建议有关。

问题基本上似乎与目录权限有关。显然,像 iZip 这样的一些应用程序会在原地打开共享文件,因此需要获得许可才能这样做。从这里https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html 可以看出,Documents/ 目录“可以通过文件共享提供给用户”,而 tmp/ 目录受到更多限制。

所以我没有使用 tmp/ 目录,而是尝试使用 Documents/ 目录,问题就解决了。感谢 iZip 支持他们的快速响应并突出显示权限问题。

出于兴趣,在使用 /Documents 文件夹时,提供了更多与其他应用程序共享的选项。

这里还有一篇关于应用扩展如何工作(托管应用、应用扩展和容器应用)的有用文章: https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html

【讨论】:

    猜你喜欢
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2018-04-30
    • 2022-11-18
    相关资源
    最近更新 更多