Thursday, 9 January 2020

Is it possible to download a pdf from a authenticated session in wkwebview?

This question was asked before. But my requirement is different. I have a workflow like this

  1. User login to app using WKWebView()
  2. User has an invoice tab after logging in through which he can download invoices by clicking a button.
  3. The issue arises here. While desktop and android apps are able to download pdf , In iOS it's not downloading. I tried many workarounds like "decidePolicyFor", download pdf once its URL is known etc.
  4. The problem which I think is normal pdf files which needed to be downloaded usually has a ".pdf" at the end of its URL.. ex- "www.xyz.com/books.pdf", However mine is like this - "https://ift.tt/36rDJlE"
  5. It may be also because the server give access to pdf only if it is authenticated. which is not the case if I try to download using URL directly

Any help will be appreciated

My CodeBase till now

import UIKit
import WebKit
class WebKitViewController: UIViewController {
    @IBOutlet weak var webView: WKWebView!
    var pdfUrl:URL!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setViewContext()
    }

    func setViewContext()  {
        let url = URL(string: "https://www.example.com")!
        webView.navigationDelegate = self
        webView.load(URLRequest(url: url))
    }

    func downloadPDF(fromUrl url:String)  {
        guard let url = URL(string: url) else { return }

               let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())

               let downloadTask = urlSession.downloadTask(with: url)
               downloadTask.resume()

    }
    @IBAction func openPDFButtonPressed(_ sender: Any) {
        let pdfViewController = PDFViewController()
        pdfViewController.pdfURL = self.pdfUrl
        present(pdfViewController, animated: false, completion: nil)
    }}
extension WebKitViewController:WKNavigationDelegate{
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let host = navigationAction.request.url {
            if host.absoluteString.contains("https://www.example.com/en/my-account/invoicePDF/"){
                 decisionHandler(.cancel)
                print(host.absoluteString)
                self.downloadPDF(fromUrl: host.absoluteString)
                return
            }
           }
           decisionHandler(.allow)

    }
}


extension WebKitViewController:  URLSessionDownloadDelegate {
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("downloadLocation:", location)
        do {
            let documentsURL = try
                FileManager.default.url(for: .documentDirectory,
                                        in: .userDomainMask,
                                        appropriateFor: nil,
                                        create: false)

            let savedURL = documentsURL.appendingPathComponent("yourCustomName90.pdf")
            self.pdfUrl = savedURL
            try FileManager.default.moveItem(at: location, to: savedURL)

        } catch {
            print ("file error: \(error)")
        }
    }
}

Note - The pdf downloads in macOS safari



from Is it possible to download a pdf from a authenticated session in wkwebview?

No comments:

Post a Comment