我们不允许永久存储文件,但允许客户自己决定是否要将其保存在不同的应用程序中/将其发送到某个地方。听起来像是 UIDocumentInteractionController
的工作,但没有持久性部分。
Apple 在文档中指出:
"[…]you can use a document interaction controller with a file that was synced to your app’s Documents/Shared directory."
是否可以直接打开文件而不将其作为文件存储在 UIDocumentInteractionController
中?
我们已经将 PDF/图像作为数据。有没有办法将数据编码为 URL 并使用它来打开交互 Controller ?
最佳答案
这个做不到,只能共享本地资源。
您可以做的是将文件保存在临时文件夹中并访问它。以下是如何实现此目的的代码。查看评论以获取描述:
import UIKit
class ViewController: UIViewController {
let sharingController = UIDocumentInteractionController()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let pdfURL = Bundle.main.url(forResource: "yourPDF", withExtension: "pdf") {
do {
let pdfData = try Data(contentsOf: pdfURL)
// create a destination url to save your pdf data
let destURL = FileManager.default.temporaryDirectory.appendingPathComponent("tempFile.pdf")
// write your pdf data to the destinastion url
do {
try pdfData.write(to: destURL)
// share your url using UIDocumentInteractionController
shareURL(url: destURL)
} catch {
print(error)
}
} catch {
print(error)
}
}
}
func shareURL(url: URL){
sharingController.url = url
sharingController.uti = "public.data, public.content"
sharingController.name = url.lastPathComponent
sharingController.presentOptionsMenu(from: view.frame, in: view, animated: true)
}
}
也不需要删除该文件,因为它位于临时目录中。
关于ios - 如何在 UIDocumentInteractionController 中打开内存不足的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869306/