Finally, I figured out the perfect solution.
In the shouldStartLoadWith:
var checkingProcesses: [URL:CheckingProcess] = [:]
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
Logger.log("WebviewViewController", "shouldStartLoadWith", "with url: \(String(describing: request.url?.absoluteString))")
self.loadingViewLogic(hide: false)
if let currentURL = request.url {
if let process = self.checkingProcesses[currentURL]{
Logger.log("WebviewViewController", "shouldStartLoadWith", "Known process: \(String(describing: process))")
if(process.status == CheckingStatus.finished){
if(process.filePath != nil){
self.openFileByPresentingOptions(pathURL: process.filePath!)
return false
}else if(process.errorMessage != nil){
//error
Util.showAlert(context: self, title: "Error getting URL type", message: process.errorMessage!, alertActions: nil)
process.clearResults()//In order to try the next time the user asks.
self.loadingViewLogic(hide: true)
return false
}else{
//link - open it
return true
}
}else if(process.status == CheckingStatus.processing){
Util.showAlert(context: self, title: "Processing...", message: "Please wait...", alertActions: nil)
return false
}else{//inactive
self.checkingProcesses[currentURL]?.startCheck()
return false
}
}else{// did not create the process yet
Logger.log("WebviewViewController", "shouldStartLoadWith", "Creating new process for URL: \(currentURL.absoluteString)")
self.checkingProcesses[currentURL] = CheckingProcess(url: currentURL, webView: self.webView)
self.checkingProcesses[currentURL]?.startCheck()
return false
}
}else{
Logger.log("WebviewViewController", "shouldStartLoadWith", "Couldn't get the currentURL")
self.loadingViewLogic(hide: true)
return false
}
}
I created a class "CheckingProcess.swift":
import UIKit
public enum CheckingStatus {
case inactive
case processing
case finished
}
class CheckingProcess: CustomStringConvertible {
var status : CheckingStatus;
var url: URL;
var filePath: URL? = nil;
var errorMessage: String? = nil;
let webView: UIWebView?
init(url: URL, webView: UIWebView) {
Logger.log("CheckingProcess", "init", "with url: \(url.absoluteString)")
self.status = CheckingStatus.inactive;
self.url = url;
self.webView = webView
}
func startCheck(){
Logger.log("CheckingProcess", "startCheck", "with url: \(self.url.absoluteString), START")
self.status = CheckingStatus.processing
let destinationUrl = Util.generateLocalFileURL(remoteURL: self.url)
DownloadManager.downloadFileAndSaveIfNotHTML(url: url, to: destinationUrl, completion: {(finalLocalURL, errorMessage) in
Logger.log("CheckingProcess", "startCheck callback block", "url: \(self.url.absoluteString), FinalLocalURL: \(String(describing: finalLocalURL)), errorMessage: \(String(describing: errorMessage))")
self.filePath = finalLocalURL
self.errorMessage = errorMessage
self.status = CheckingStatus.finished
self.webView?.loadRequest(NSURLRequest(url: self.url as URL) as URLRequest)
})
}
func clearResults(){
self.status = CheckingStatus.inactive
self.filePath = nil
self.errorMessage = nil
}
public var description: String {
return "URL: \(self.url), Status: \(self.status), filePath: \(String(describing: self.filePath)), errorMessage: \(String(describing: self.errorMessage))"
}
}
Enjoy!