我尝试通过以下方式打开文档文件,但没有任何反应。
我也尝试过其他方法,但它对我不起作用。打开文档文件(doc、docx、pdf、ppt 等)的正确方法是什么。我在 UIWebView()
的某处读到这也可以完成,那么更好的方法是什么?
let fileURL = URL(fileURLWithPath: documentMessage.fileUrl)
let doc = DocumentPreview(fileURL)
doc.startPreview()
文档预览.swift
class DocumentPreview : NSObject, UIDocumentInteractionControllerDelegate {
var url : URL?
var document : UIDocumentInteractionController?
var previewVC : UINavigationController?
init(_ url: URL) {
super.init()
self.url = url
document = UIDocumentInteractionController(url:url)
document?.delegate = self
}
func startPreview(){
document?.presentPreview(animated:true)
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return previewVC!
}
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
print("end Preview")
previewVC!.popViewController(animated: true)
}
}
最佳答案
看一下 documentMessage。如果 .fileUrl 属性是 URL,则此代码是错误的:
let fileURL = URL(fileURLWithPath: documentMessage.fileUrl)
最简单的修复是这样的:
let fileURL = documentMessage.fileUrl
关于ios - UIDocument 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943212/