This question was asked before. But my requirement is different. I have a workflow like this
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
With func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { , you get the file url to download. Then download it with JS.
Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.
Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.
Try to read cookies from webview and save it before making a call to download your pdf:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    self.webView.configuration.websiteDataStore.httpCookieStore.getAllCookies { cookies in
       //save cookies
       self.cookies = cookies
    }
}
func downloadPDF(fromUrl url:String)  {
    guard let url = URL(string: url) else { return }
    HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL:nil)
    ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With