Created
May 25, 2020 18:10
-
-
Save revblaze/a522c3b6dda86ea5631644092417357c to your computer and use it in GitHub Desktop.
WebKit Observer: Page Load Progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var webViewProgressObserver: NSKeyValueObservation? | |
@IBOutlet var progressBar: UIProgressView! // iOS Progress Bar | |
@IBOutlet var progressBar: NSProgressIndicator! // macOS Progress Spinner | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
webViewProgressObserver = webView.observe(\.estimatedProgress, options: .new) { [weak self] webView, change in | |
self?.progressDidChange(progress: change.newValue ?? 1.0) | |
} | |
} | |
func progressDidChange(progress: Double) { | |
let value = Float(progress) // Set value as Float for Progress Indicator | |
if value < 1 { // While page is loading: | |
progressBar.isHidden = false // Show progress bar | |
progressBar.progress = value // Set value of progress indicator | |
} else { // When page is done loading: | |
progressBar.isHidden = true // Hide progress bar | |
} | |
print("Loading: \(value)") // Print page load progress to console | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment