如何使用单个progressView更新一个或多个wkwebviews的估计进度

时间:2018-07-18 02:49:38

标签: ios swift wkwebview uiprogressview

我有一个通过情节提要配置的视图控制器。视图顶部有一个progressView。该视图还具有一个称为contentView的UIView,该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
    }

0 个答案:

没有答案