简体   繁体   English

无法通过打开方式从 iOS 文件应用程序打开文件<My App>共享表:提供的文件 URL 中不存在文件

[英]Cannot open file from iOS Files app via the Open In <My App> share sheet: no file at the provided file URL exists

My iOS app can open CSV files, in order to import their data.我的 iOS 应用程序可以打开 CSV 文件,以便导入它们的数据。 I can open files from within the app via a UIDocumentPickerViewController with no issues, selecting a file shown in the Files app.我可以通过UIDocumentPickerViewController从应用程序中打开文件,没有问题,选择文件应用程序中显示的文件。 However, when viewing a file in the Files app first, and then opening my app from there (via the Open In share sheet), my app cannot see the file at the URL passed to my app.但是,当首先在“文件”应用程序中查看文件,然后从那里打开我的应用程序(通过“打开方式”共享表)时,我的应用程序无法在传递给我的应用程序的 URL 处看到该文件。 The file does not seem to exist.该文件似乎不存在。

I added the following debug code to my AppDelegate:我在 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
}

When I open a file from Files in this app, it results in a log line like:当我从这个应用程序的文件中打开一个文件时,它会产生如下日志行:

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

When I open a file from some other app (eg Dropbox, or from an Email), the file can be processed, and the following is logged:当我从其他应用程序(例如 Dropbox 或电子邮件)打开文件时,可以处理该文件,并记录以下内容:

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

Note the different path ( Documents/Inbox vs Mobile Documents/com~apple~CloudDocs ).注意不同的路径( Documents/Inbox vs Mobile Documents/com~apple~CloudDocs )。 What is causing this?这是什么原因造成的? How can I support the opening of files from the Files app in my app?我如何支持从我的应用程序中的文件应用程序打开文件?

The document type(s) supported by my app are as follows:我的应用支持的文档类型如下:

Xcode 文档类型

I fixed this by adjusting some settings in the app's Info.plist.我通过调整应用程序的 Info.plist 中的一些设置解决了这个问题。

I switched UISupportsDocumentBrowser to false , and added LSSupportsOpeningDocumentsInPlace , also set to false .我将UISupportsDocumentBrowser切换为false ,并添加了LSSupportsOpeningDocumentsInPlace ,也设置为false

I think these settings were set incorrectly (by me) after I received an email from App Store Connect stating:在我收到来自 App Store Connect 的一封电子邮件后,我认为这些设置(由我)设置不正确:

Invalid Document Configuration - Document Based Apps should support either the Document Browser (UISupportsDocumentBrowser = YES) or implement Open In Place (LSSupportsOpeningDocumentsInPlace = YES/NO).无效的文档配置 - 基于文档的应用程序应支持文档浏览器 (UISupportsDocumentBrowser = YES) 或实现就地打开 (LSSupportsOpeningDocumentsInPlace = YES/NO)。 Visit https://developer.apple.com/document-based-apps/ for more information.访问https://developer.apple.com/document-based-apps/了解更多信息。

Setting the LSSupportsOpeningDocumentsInPlace flag to false prevents the app to be automatically opened when selecting the file in the Files App.LSSupportsOpeningDocumentsInPlace标志设置为 false 可防止在“文件”应用程序中选择文件时自动打开应用程序。 In case the the file is opened in place one needs to request/unlock access first:如果文件在原地打开,需要先请求/解锁访问权限:

 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
 }

I don't think you can share files between different apps (that are not in the same app group):我认为您不能在不同的应用程序(不在同一个应用程序组中)之间共享文件:

Sharing data between apps on iOS 在 iOS 上的应用程序之间共享数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM