无法通过“在<我的应用程序>”中打开“打开”文件从iOS文件应用程序打开文件:提供的文件URL上没有文件

And*_*net 4 nsfilemanager ios icloud swift

我的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
}
Run Code Online (Sandbox Code Playgroud)

当我从此应用程序中的“文件”中打开文件时,它会导致出现如下日志行:

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

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

存在:true(/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/Inboxvs Mobile Documents/com~apple~CloudDocs)。是什么原因造成的?如何在我的应用程序中支持通过“文件”应用程序打开文件?

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

Xcode文件类型

cro*_*m87 8

将标志设置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
 }
Run Code Online (Sandbox Code Playgroud)


And*_*net 6

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

我切换UISupportsDocumentBrowserfalse,并添加LSSupportsOpeningDocumentsInPlace,也设置为false

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

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