ios - UIDocument 不起作用

标签 ios swift uidocumentinteraction

我尝试通过以下方式打开文档文件,但没有任何反应。 我也尝试过其他方法,但它对我不起作用。打开文档文件(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/

相关文章:

ios - UIDocumentInteractionController 在 iPad 模拟器 (XCode 3.2.3) 中不工作

ios - 如何在 swift 3 中将 MKPolylines 添加到 MKSnapShotter?

swift - 如何在 SpriteKit 中将一个 Action 应用于多个 Sprite?

objective-c - Titanium 上的文档交互 Controller

swift - 使用动画设置 WKInterfacePicker 高度选择任意项目

Swift 在 println 的控制台中给我一条错误消息

ios - iOS自定义文档编辑器可以将文件返回到打开的应用程序吗?

ios - 如何将 ImageView 及其属性从一个 View Controller 传递到另一个 View Controller ?

ios - Swift 中的 Twitter 时间线集成 (SwiftHTTP)

ios - UINavigationBar标题无法正确设置