How do I display the loading progress for a UIWebV

2019-08-11 16:24发布

Im looking to implement a method that dropbox uses for there iOS app, where they have the actual progress view for a UIWebView on a view that is shown right before the actual UIWebView is shown.

The video in this link shows exactly what I am trying to say.

Now just as they have a masterView -> progessView -> PdfViewer setup , I do as well. So I guess the question is, how did they pass the data from the tableView, onto the progress view, and display the UIWebView? Does the progress view conform to the UIWebViewDelegate to be able to call webViewDidStartLoad(webView: UIWebView) ? and have a variable representing the webView and pass along the data to the UIWebView itself?

Here is the code for my UIWebView:

class PdfViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet var myWebView: UIWebView!

    @IBOutlet var progressView: UIProgressView!

    var hasFinishedLoading = false

    var selectedContent: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        myWebView.delegate = self

        let url: NSURL! = NSURL(string: selectedContent)
        myWebView.loadRequest(NSURLRequest(URL: url))

    }

    func webViewDidStartLoad(webView: UIWebView) {
        var hasFinishedLoading = false

        updateProgress()
    }

    func webViewDidFinishLoad(webView: UIWebView) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
            dispatch_get_main_queue()) {
                [weak self] in
                if let _self = self {
                    _self.hasFinishedLoading = true
            }
        }
    }

    deinit {
        myWebView.stopLoading()
        myWebView.delegate = nil
    }

    func updateProgress(){
        if progressView.progress >= 1 {
            progressView.hidden = true
        } else {
            if hasFinishedLoading {
                progressView.progress += 0.002
            } else {
                if progressView.progress <= 0.3 {
                    progressView.progress += 0.004
                } else if progressView.progress <= 0.6 {
                    progressView.progress += 0.002
                } else if progressView.progress <= 0.9 {
                    progressView.progress += 0.001
                } else if progressView.progress <= 0.94 {
                    progressView.progress += 0.0001
                } else {
                    progressView.progress = 0.9401
                }
            }

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.008 * Double(NSEC_PER_SEC))),
                dispatch_get_main_queue()) {
                    [weak self] in
                    if let _self = self {
                        _self.updateProgress()
                    }
             }
      }
}

}

Now how do I take this code, and instead pass it to my progessViewController somehow to let it show the progress rather than it being shown in my WebView? and when the progress complete, than instantiate and show the loaded UIWebView page?

Ant suggestions would be appreciated. Thanks

1条回答
Evening l夕情丶
2楼-- · 2019-08-11 16:49

You can add this way in this method!

func webViewDidFinishLoad(webView: UIWebView) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
        dispatch_get_main_queue()) {
            [weak self] in
            if let _self = self {
                _self.hasFinishedLoading = true

                //Add here!
                _self.view.addSubview(webView)
            }
    }
}

You can do other approach, in viewWillAppear you can hide the web view and when finish load you can set hidden false.

Hope helped you!

查看更多
登录 后发表回答