0

I'm trying to create an iOS WebView app that loads a site and allows users to log in and make purchases. I'm trying to have a progress bar display when loading pages but the progress doesn't display at all and I'm not sure where I'm going wrong. I also have a pull to refresh view but without a progress bar I can't be sure this is working either. This is my code, any help would be much appreciated!

import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
// Define Views
var webView: WKWebView!
var progressView: UIProgressView!

override func loadView() {
    // Load Initial WebView
    webView = WKWebView()
    webView.navigationDelegate = self
    view = webView
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Create Progress View
    progressView = UIProgressView(progressViewStyle: .default)
    progressView.sizeToFit()
    let progressButton = UIBarButtonItem(customView: progressView)
    toolbarItems = [progressButton]
    navigationController?.isToolbarHidden = false
    //Set and Load Initial URL
    let url = URL(string: "https://shop.nygmarose.com/")!
    webView.load(URLRequest(url: url))
    // Set WebView Config
    webView.allowsBackForwardNavigationGestures = true
    webView.scrollView.isScrollEnabled = true
    webView.scrollView.bounces = true
    // Allow Scroll to Refresh
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
    webView.scrollView.addSubview(refreshControl)
    webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
}

func refreshWebView() {
    // On Scroll to Refresh, Reload Current Page
    webView.reload()
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    // Display Progress Bar While Loading Pages
    if keyPath == "estimatedProgress" {
        progressView.progress = Float(webView.estimatedProgress)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}
2
  • I have checked it's woking perfect @Musa Jul 5, 2017 at 12:56
  • @iOSDev It's working for you? It could be a problem with my xcode emulator I suppose
    – Arran
    Jul 5, 2017 at 13:01

2 Answers 2

0

I have checked your code and it's working perfect, check here is the output

let me know for more.

enter image description here

1
  • Thanks for that, it isn't displaying in my emulator but it must be my end.
    – Arran
    Jul 5, 2017 at 13:12
0

Did you consider to force the use of the main thread? UI updates are only guaranteed on the main thread.

For example:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    // Display Progress Bar While Loading Pages
    if keyPath == "estimatedProgress" {

        // force main thread async call
        DispatchQueue.main.async(execute: {

            // do UI update
            progressView.progress = Float(webView.estimatedProgress)

        })
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.