Ios 如何使用单个progressView更新一个或多个Web视图的估计进度
我有一个通过故事板配置的视图控制器。视图的顶部有一个progressView。该视图还有一个名为contentView的UIView,它被用作其他视图的占位符以进行约束。问题是,当新的wkwebview(即docUploadWebView)作为子视图添加到主视图时,progressView不再工作。发现docUploadWebView没有调用(覆盖函数observeValue)。 但我不确定我缺少了什么来获得一个独立的进度视图来显示从一个或多个Web视图加载页面的进度 提前谢谢。我在下面添加了大部分有问题的代码Ios 如何使用单个progressView更新一个或多个Web视图的估计进度,ios,swift,wkwebview,uiprogressview,Ios,Swift,Wkwebview,Uiprogressview,我有一个通过故事板配置的视图控制器。视图的顶部有一个progressView。该视图还有一个名为contentView的UIView,它被用作其他视图的占位符以进行约束。问题是,当新的wkwebview(即docUploadWebView)作为子视图添加到主视图时,progressView不再工作。发现docUploadWebView没有调用(覆盖函数observeValue)。 但我不确定我缺少了什么来获得一个独立的进度视图来显示从一个或多个Web视图加载页面的进度 提前谢谢。我在下面添加了大
// 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
}
你知道进展情况吗?它允许进度视图合并多个进程的进度。也许在某些方面会有用。@matt谢谢。我其实不知道。我会调查的。你能告诉我,如果你已经通过了,为什么当前的代码不能工作。