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

时间:2015-04-14 15:24:02

标签: swift pdf webview ibooks

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

var docController:UIDocumentInteractionController?

如果让路径= 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")
    }

}

}

1 个答案:

答案 0 :(得分:-1)

在ibooks中打开之前,您需要将pdf文件写入设备。以下是有效的代码:

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()
        })
    })
}