0

I have created a whole URL request, and I load it in the web view I am using. In that process, there comes a web page, where on click of a link, a pdf has to be downloaded. Since, no URL is associated with that file, I am not able to download it(Request gets fired on clicking the link, and in it's response, I only get the file name). Hence I thought of opening that link(URL Request) in browser so that it can manage on its own. I tried https://developer.apple.com/forums/thread/108394 this as well, but while downloading, the status code was 404, maybe due to invalid URL. I intercept the request inside

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
                
        let relativePath = navigationAction.request.url?.relativePath

        switch fetchRelativePathType(forPath: relativePath ?? "") {
        case .goToLogin:
            proceedToLogin()
            decisionHandler(.cancel)
        case .goToDashboard:
            cancel()
            decisionHandler(.cancel)
        case .downloadPDF:
            UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
            decisionHandler(.cancel)
        case .noneMatched:
            decisionHandler(.allow)
        }
        
    }

The browser opens and as expected Error page came into view, as it isn't getting the whole request to be loaded. My ultimate goal is to download that pdf in iPhone local storage.

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
Anuranjan Bose
  • 201
  • 1
  • 3
  • 16
  • I'm not sure what you mean by "no URL is associated with that file". There must be a URL for the file, otherwise you can't download it. How the request gets fired (via JavaScript or a direct link in HTML) isn't relevant, the process for downloading is the same. – Hannes Hertach Jul 07 '20 at 08:39
  • On clicking the link "https://www.some.com/final/preview" gets called. In browser, it automatically downloads the file in local system. But, while clicking the same link in web view in app, the request is being made, but wasn't doing anything. So, I intercepted the response, and tried downloading it by appending filename with url. But failed. – Anuranjan Bose Jul 07 '20 at 08:47
  • Your browser must be getting the URL from somewhere. You could try using the "sources" or "network" tabs in your browsers devtools on desktop to find what the URL looks like. – Hannes Hertach Jul 07 '20 at 09:18

0 Answers0