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.