如何使用Swift将带有pdf的webview页面加载到ibook中 [英] How do I load a webview page with a pdf into ibooks using Swift
本文介绍了如何使用Swift将带有pdf的webview页面加载到ibook中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我能找到的最接近的代码,但没有使用包含pdf文件的webview页面的语法.
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?
var docController: UIDocumentInteractionController?
如果让路径= NSBundle.mainBundle().pathForResource("book",ofType:"pdf"){ 如果让targetURL = NSURL.fileURLWithPath(path){
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")
}
}
}
推荐答案
发现在ibook中打开之前,您需要将pdf文件写入设备.这是起作用的代码:
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()
})
})
}
这篇关于如何使用Swift将带有pdf的webview页面加载到ibook中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文