WKWebView将pdf保存到ibooks,从链接保存pdf

时间:2016-11-08 15:48:43

标签: ios swift pdf webview wkwebview

我是初级的,目前正在开发我在WKWebView中的项目,并且有打开pdf的链接。我可以在Safari中打开它,然后在iBooks中打开,但我希望它能在我的应用程序中完成。可能吗 ?

这是照片的样子:

我可以选择pdf的图片

description1

想象它会打开什么

description2 我的webview课程

class WebVC: UIViewController, WKUIDelegate {



var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    let myURL = NSURL(string: "\(savedURL!)")
    let myRequest = URLRequest(url: myURL! as URL)
    webView.load(myRequest)
    webView.allowsBackForwardNavigationGestures = true
    webView.allowsLinkPreview = false
}

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.toolbar.isHidden = false
}

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: CGRect(x: 100, y: 100, width: 110, height: 110), configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView

}

@IBAction func logoutPressed(_ sender: AnyObject) {
    defaults.set(false, forKey: "isLogged")
    defaults.set("EMPTY URL", forKey: "savedURL")
    _ = self.navigationController?.popToRootViewController(animated: true)
}


@IBAction func goBack(_ sender: Any?) {
    if (self.webView.canGoBack) {
        self.webView.goBack()
    }
}

}

1 个答案:

答案 0 :(得分:1)

我用UIDocumentInteractionController解决了它,添加一个按钮,当我按下按钮它将下载整个页面,然后呈现DocumentController,其中"导入到iBooks"选项是。希望它有所帮助。

@IBAction func shareBtn(_ sender: AnyObject) {
    var localPath: NSURL?

    Alamofire.download(webView.url!, method: .get, parameters: nil, headers: nil) { (tempUrl, response)  in

        let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let pathComponent = response.suggestedFilename

        localPath = directoryURL.appendingPathComponent(pathComponent!) as NSURL?
        return (destinationURL: localPath as! URL, options: .removePreviousFile)


        }.response { response in
            if localPath != nil{


                self.docController = UIDocumentInteractionController(url: localPath! as URL)
                self.docController.presentOptionsMenu(from: sender as! UIBarButtonItem, animated: true)

            }
    }
}