How do I load a webview page with a pdf into ibooks using Swift

人盡茶涼 提交于 2019-12-11 02:35:56

问题


This is closest code I can find but doesn't have syntax for using the webview page that contains the pdf file.

var docController: UIDocumentInteractionController?

if let path = NSBundle.mainBundle().pathForResource("book", ofType: "pdf") { if let 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:


Found out you need to write the pdf file to device before opening in ibooks. Here is the code that worked:

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


来源:https://stackoverflow.com/questions/29631293/how-do-i-load-a-webview-page-with-a-pdf-into-ibooks-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!