swift - 如何使用 Swift 将带有 pdf 的 webview 页面加载到 ibooks 中

标签 swift pdf webview ibooks

这是我能找到的最接近的代码,但没有使用包含 pdf 文件的 webview 页面的语法。

var docController: UIDocumentInteractionController?

if let path = NSBundle.mainBundle().pathForResource("book", ofType: "pdf") { 如果让 targetURL = NSURL.fileURLWithPath(path) {

    docController = UIDocumentInteractionController(URL: targetURL)
    let url = NSURL(string:"itms-books:");

    if UIApplication.sharedApplication().canOpenURL(url!) {

        docController!.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)

        println("iBooks is installed")

        }else{

        println("iBooks is not installed")
    }

}

最佳答案

发现您需要先将 pdf 文件写入设备,然后才能在 ibooks 中打开。这是有效的代码:

func downloadPDF() {
    // Running operations that takes a long time in a background thread is recommended
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
        // Get the PDF data from the URL
        let url = self.webview.request?.URL
        let pdfURL = url?.absoluteString
        let pdfData = NSData(contentsOfURL: NSURL(string: pdfURL!)!)!

        // Store the data locally as a PDF file in the Documents directory
        let documentsDirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
        localPdfPath = documentsDirPath.stringByAppendingPathComponent(pdfURL!.lastPathComponent)
        pdfData.writeToFile(localPdfPath, atomically: true)

        // UI related stuff should be called in the main thread.
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.openIniBooks()
            IJProgressView.shared.hideProgressView()
        })
    })
}

关于swift - 如何使用 Swift 将带有 pdf 的 webview 页面加载到 ibooks 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29631293/

相关文章:

swift - 为什么我收到“没有成员(member)”错误

ios - 带有辅助类的 xib 文件按钮不起作用

ios - PoDoFo 库在 ios 7 中崩溃但在 ios 6 及更早版本中工作

parsing - 如何找到预告片字典?

cordova - 从 IONIC Native InAppBrowser 隐藏位置工具栏

ios - Swift tableView.dequeueReusableCell 从不返回 Nil

ios - 覆盖 UICollectionViewCell 的 estimatedItemSize

javascript - 将 HTML 转换为 Word,然后转换为 pdf

java - 我怎样才能在加载完成后只显示 webview fragment

android - 如何从 Android 中的 Chrome 选项卡获取回调,例如 web View 提供了 shouldOverrideUrlLoading、onLoadResource、onPageFinished