I have a view controller configured via storyboard. Top of the view has a progressView. The view also has a UIView called contentView, which is being used as a placeholder for other view to constrain against. The problem is when a new wkwebview i.e docUploadWebView is added as a subview to the main view the progressView does not work anymore. Figured out that the (override function observeValue) is not getting called for the docUploadWebView. But not sure what I am missing to get an independent progress view to show progress for page loads from one or more webviews.
Thanks in advance. I have added most of the code in question below.
// class properties
let webView: WKWebView = {
let userContentController = WKUserContentController()
let sharedProcessPool = WKProcessPool()
let configuration = WKWebViewConfiguration()
configuration.processPool = sharedProcessPool
return WKWebView(frame: .zero, configuration: configuration)
}()
var docUploadWebView = WKWebView()
// END class properties
override func loadView() {
super.loadView()
let webview = self.webView
view.addSubview(webview)
webview.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: webview, attribute: .height, relatedBy: .equal, toItem: self.contentView, attribute: .height, multiplier: 1, constant: 0)
let width = NSLayoutConstraint(item: webview, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1, constant: 0)
let offset = NSLayoutConstraint(item: webview, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 0)
view.addConstraints([height, width, offset])
}
override func viewDidLoad() {
super.viewDidLoad()
guard let app = app else { return }
let urlToLoad = app.appUrlString
var request = URLRequest(url: URL(string: urlToLoad!)!)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let allcookies = appDelegate.getAllExistingCookiesInHTTPCookieStorage()
self.syncAndInjectJSCookies()
webView.allowsBackForwardNavigationGestures = true
webView.navigationDelegate = self
webView.uiDelegate = self
webView.load(request, with: allcookies!)
self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
self.docUploadWebView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == keyPathToObserve {
if let object = object as! WKWebView? {
progressView.progress = Float(object.estimatedProgress )
}
}
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
let url = navigationAction.request.url
let profileUrl = profile?.systemUrl
if url?.absoluteString.range(of:profileUrl!) != nil {
if url?.absoluteString.range(of: "fileupload.jsp") != nil {
return getDocUploadView(webView: webView, configuration: configuration)
}
self.webView.load(navigationAction.request)
} else {
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
}
return nil
}
private func getDocUploadView(webView: WKWebView, configuration: WKWebViewConfiguration) -> WKWebView {
docUploadWebView = WKWebView(frame: webView.frame, configuration: configuration)
view.addSubview(docUploadWebView)
docUploadWebView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: docUploadWebView, attribute: .height, relatedBy: .equal, toItem: self.contentView, attribute: .height, multiplier: 1, constant: 0)
let width = NSLayoutConstraint(item: docUploadWebView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1, constant: 0)
let offset = NSLayoutConstraint(item: docUploadWebView, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 0)
view.addConstraints([height, width, offset])
docUploadWebView.uiDelegate = self
docUploadWebView.navigationDelegate = self
return self.docUploadWebView
}