【问题标题】:Cannot open file from iOS Files app via the Open In <My App> share sheet: no file at the provided file URL exists无法通过在 <我的应用程序> 共享表中打开从 iOS 文件应用程序打开文件:提供的文件 URL 中不存在文件
【发布时间】:2019-06-24 08:29:22
【问题描述】:

我的 iOS 应用可以打开 CSV 文件,以便导入其数据。我可以通过UIDocumentPickerViewController 在应用程序内毫无问题地打开文件,选择文件应用程序中显示的文件。但是,当首先在“文件”应用程序中查看文件,然后从那里打开我的应用程序(通过“打开方式”共享表)时,我的应用程序无法在传递给我的应用程序的 URL 中看到该文件。该文件似乎不存在。

我将以下调试代码添加到我的 AppDelegate:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    print("Exists: \(FileManager.default.fileExists(atPath: url.path)) (\(url.path))")
    return true
}

当我在这个应用程序中打开文件时,它会产生如下日志行:

存在:假 (/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/Reading List - Andrew’s iPhone - 2018-10-18 11-19.csv)

当我从其他应用程序(例如 Dropbox 或电子邮件)打开文件时,可以处理该文件,并记录以下内容:

存在:真 (/private/var/mobile/Containers/Data/Application/F9110F90-7A91-4AB6-A92E-0ED933184EA4/Documents/Inbox/Reading List - Andrew’s iPhone - 2018-01-27 08-03.csv)

注意不同的路径(Documents/Inbox vs Mobile Documents/com~apple~CloudDocs)。这是什么原因造成的?如何在我的应用中支持从文件应用打开文件?

我的应用支持的文档类型如下:

【问题讨论】:

    标签: ios swift icloud nsfilemanager


    【解决方案1】:

    我通过调整应用程序的 Info.plist 中的一些设置来解决此问题。

    我将UISupportsDocumentBrowser 切换为false,并添加了LSSupportsOpeningDocumentsInPlace,也设置为false

    在我收到来自 App Store Connect 的电子邮件后,我认为这些设置(我自己)设置不正确:

    无效的文档配置 - 基于文档的应用程序应支持文档浏览器 (UISupportsDocumentBrowser = YES) 或实施就地打开 (LSSupportsOpeningDocumentsInPlace = YES/NO)。访问https://developer.apple.com/document-based-apps/了解更多信息。

    【讨论】:

    • 谢谢,它成功了。对我来说,将 LSSupportsOpeningDocumentsInPlace 设置为 false 就足够了
    【解决方案2】:

    LSSupportsOpeningDocumentsInPlace 标志设置为 false 可防止在文件应用程序中选择文件时自动打开应用程序。如果文件在适当位置打开,则需要先请求/解锁访问权限:

     func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {     
       let opensInPlace = options[.openInPlace] != nil
       opensInPlace ? url.startAccessingSecurityScopedResource() : nil
       let fileExists = FileManager.default.fileExists(atPath: url.path)  
       opensInPlace ? url.stopAccessingSecurityScopedResource() : nil
     }
    

    【讨论】:

      【解决方案3】:

      我认为您不能在不同的应用程序(不在同一个应用程序组中)之间共享文件:

      Sharing data between apps on iOS

      【讨论】:

      • 嗯,该应用确实成功地从其他应用(邮件、Dropbox)打开了文件,但不是从“文件”应用
      猜你喜欢
      • 1970-01-01
      • 2013-02-28
      • 2012-12-24
      • 2018-11-19
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      相关资源
      最近更新 更多