首页 > 解决方案 > 从“文件”应用程序长按复制复制目录时,Swift FileManger 不包含内容

问题描述

在“文件”应用程序中,我可以使用“选择”选项将文件或文件目录导入我的应用程序,然后选择目录/文件,然后使用共享按钮,选择我的应用程序。然后该应用程序打开并运行我的importURL功能,一切正常。(这部分依赖于拥有正确的尿路感染等)。

但是,如果在“文件”应用程序中,我长按一个文件夹,然后“复制”该文件夹。然后我切换回我的应用程序并粘贴运行完全相同的代码,它只是复制文件夹,其中没有任何文件。

有没有办法让这个副本/导入从“文件”应用程序“复制”中工作?

发生错误时,XCode 或应用程序中不会报告错误。

        coordinator.coordinate(readingItemAt: url, options: .withoutChanges, error: &err) { (coordinatedURL) in
            do {
                try FileManager.default.copyItem(at: url, to: AppDelegate.docsDir.appendingPathComponent(url.lastPathComponent))
                DispatchQueue.main.async {
                    self.refreshData()
                }
            } catch {
                DispatchQueue.main.async {
                    let alert = UIAlertController(title: "Cannot Copy Data Source", message: "Unable to copy from:\n\(url)\n\nError details:\n\n\(error)", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
                    self.present(alert, animated: true)
                }
            }
        }
        if isSecure {
            url.stopAccessingSecurityScopedResource()
        }

出现问题时,我从粘贴板中获取 URL,如下所示:

            guard let itemType = UIPasteboard.general.types.first else { return }
            guard let pasteItemProvider = UIPasteboard.general.itemProviders.first else { return }
                pasteItemProvider.loadInPlaceFileRepresentation(forTypeIdentifier: itemType) { (url, ok, err) in
                    if url != nil {
                        self.importFromURL(url!)
                    } else if err != nil {
                        print("FAILED to load file by item provider's URL.  Error:\n\(err!)")
                    }
                }
            }

更新:有时也会在使用 select 方法时发生!

我发现这个问题有时会出现在上面描述的“选择”方法中,因为它是通常有效的解决方法。好吧,事实证明它也不能可靠地工作。

什么可靠地工作(到目前为止):

...正在将文件夹完全复制到“文件”应用程序中。即,选择,然后点击文件夹图标(底部工具栏),然后(仍在“文件”应用程序中),选择“在我的 iPhone 上”项目,然后在那里选择自定义应用程序的文件夹。

到目前为止,这似乎工作可靠。但是使用 NSFileCoodinator.coordinate() 和 FileManager.copyItem() 不能可靠地从“文件”应用程序复制文件夹内容。

标签: iosswiftdirectorynsfilemanager

解决方案


推荐阅读