deniskakacka deniskakacka - 22 days ago 17
Swift Question

WKWebView save pdf to ibooks, save pdf from link

I'm Junior and currently working on project where Im in WKWebView and there are links to open pdf. I can open it in Safari and then open in iBooks, but i want it to do it inside my app. Is it possible ?

Here are pics how it looks like:

picture where i can select pdf

description1

picture what it will open

description2
my webview class

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

}

Answer

I solved it with UIDocumentInteractionController, add a button and when I push the button It will download whole page and then present DocumentController where "import to iBooks" option is. Hope it helps.

@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)

            }
    }
}
Comments