我正在尝试用下面的方法打开文档文件,但是没有任何反应。我也尝试过其他方法,但对我不起作用。打开文档文件(doc、docx、pdf、ppt等)的正确方式是什么?我在某个地方读到了UIWebView(),这也是可以做到的,那么更好的方法是什么呢?
let fileURL = URL(fileURLWithPath: documentMessage.fileUrl)
let doc = DocumentPreview(fileURL)
doc.startPreview()DocumentPreview.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)
}
}发布于 2017-05-14 05:03:50
看看documentMessage吧。如果.fileUrl属性是一个URL,那么这个代码是错误的:
let fileURL = URL(fileURLWithPath: documentMessage.fileUrl)最简单的解决方法是:
let fileURL = documentMessage.fileUrlhttps://stackoverflow.com/questions/43943212
复制相似问题