首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于文档的应用程序

基于文档的应用程序
EN

Stack Overflow用户
提问于 2018-02-15 04:50:19
回答 1查看 1.2K关注 0票数 2

Xcode9/pdf 11:我的应用程序创建iOS文件,这些文件使用核心数据数据库中的数据呈现。pdf是从WebView创建的,并存储在documents目录中。到目前为止,一切都正常。我可以在苹果的文件应用程序中打开pdf文件,甚至可以在那里编辑它们。我想在我的应用程序中实现DocumentBrowserViewController。我使用了Xcode9中的模板。我不能像在苹果的文件应用程序中那样在DocumentViewController中显示创建的pdf文件。我可以选择文档,但DocumentViewController只显示文件名和一个完成按钮。在这个模板的DocumentViewController中显示现有的pdf最简单的方式是什么?我必须在哪里实现相关的代码?

来自Xcode9-template的代码:

类DocumentViewController: UIViewController {

代码语言:javascript
复制
@IBOutlet weak var documentNameLabel: UILabel!

var document: UIDocument?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Access the document
    document?.open(completionHandler: { (success) in
        if success {
            // Display the content of the document, e.g.:
            self.documentNameLabel.text = self.document?.fileURL.lastPathComponent

            // --> How to show/load the pdf here? Do I have to create a UIView here first?

        } else {
            // Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
        }
    })
}

下面是来自Xcode9-template的UIDocument的子类:

类文档: UIDocument {

代码语言:javascript
复制
var pdfData: PreviewViewController?

override func contents(forType typeName: String) throws -> Any {
    // Encode your document with an instance of NSData or NSFileWrapper
    return Data()

}

override func load(fromContents contents: Any, ofType typeName: String?) throws {
   // --> do I have to load the pdf here?
}

}

我在应用程序开发方面经验不是很丰富。我在看Paul Hagerty在iTunesU上关于UIDocument持久性的课程cs193p。但他正在保存应用程序的模型,而我有一个pdf。有没有一种简单的方法来显示pdf文件?我尝试了不同的方法来加载pdf,但我不确定是否必须首先创建一个UIView。文件应用程序的一个很好的功能是,你可以编辑或通过电子邮件发送pdf。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-15 09:34:05

UIDocumentBrowserViewController的一个很好的资源是2017年的WWDC视频:

Building Great Document-based Apps in iOS 11 - WWDC 2017

在你的func load评论中回答你的问题。是的,你可以在这里得到你的Data格式的pdf

代码语言:javascript
复制
override func load(fromContents contents: Any, ofType typeName: String?) throws {
    guard let data = contents as? Data else { return }
}

但是你应该做的是在你的DocumentViewController中加载fileURL,这是UIDocument的一个属性到一个UIWebView中,它可以很容易地显示pdf文件。看看这里:https://stackoverflow.com/a/38792456/4089350

然后在document.open函数中,将fileURL传递给UIWebView

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48795947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档