Forked from fxm90/WebViewExampleViewController.swift
Created
October 11, 2018 07:13
-
-
Save hisoka0917/e24faf0ee8a72f4f9db3137cfcd359a4 to your computer and use it in GitHub Desktop.
Show progress of WKWebView in UIProgressBar that is attached to an UINavigationBar
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
// | |
// WebViewExampleViewController.swift | |
// | |
// Created by Felix Mau on 06.01.18. | |
// Copyright © 2018 Felix Mau. All rights reserved. | |
// | |
import UIKit | |
import WebKit | |
class WebViewExampleViewController: UIViewController { | |
// MARK: - Outlets | |
/// The web view from the interface builder. | |
@IBOutlet var webView: WKWebView! | |
// MARK: - Private properties | |
/// Progress view reflecting the current loading progress of the web view. | |
let progressView = UIProgressView(progressViewStyle: .default) | |
/// The observation object for the progress of the web view (we only receive notifications until it is deallocated). | |
private var estimatedProgressObserver: NSKeyValueObservation? | |
// MARK: - Public methods | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupProgressView() | |
setupEstimatedProgressObserver() | |
if let initialUrl = URL(string: "https://felix.hamburg") { | |
setupWebview(url: initialUrl) | |
} | |
} | |
// MARK: - Private methods | |
private func setupProgressView() { | |
guard let navigationBar = navigationController?.navigationBar else { return } | |
progressView.translatesAutoresizingMaskIntoConstraints = false | |
navigationBar.addSubview(progressView) | |
progressView.isHidden = true | |
NSLayoutConstraint.activate([ | |
progressView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor), | |
progressView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor), | |
progressView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor), | |
progressView.heightAnchor.constraint(equalToConstant: 2.0) | |
]) | |
} | |
private func setupEstimatedProgressObserver() { | |
estimatedProgressObserver = webView.observe(\.estimatedProgress, options: [.new]) { [weak self] webView, _ in | |
self?.progressView.progress = Float(webView.estimatedProgress) | |
} | |
} | |
private func setupWebview(url: URL) { | |
let request = URLRequest(url: url) | |
webView.navigationDelegate = self | |
webView.load(request) | |
} | |
} | |
// MARK: - WKNavigationDelegate | |
extension WebViewExampleViewController: WKNavigationDelegate { | |
func webView(_: WKWebView, didStartProvisionalNavigation _: WKNavigation!) { | |
UIApplication.shared.isNetworkActivityIndicatorVisible = true | |
UIView.transition(with: progressView, | |
duration: 0.33, | |
options: [.transitionCrossDissolve], | |
animations: { | |
self.progressView.isHidden = false | |
}, | |
completion: nil) | |
} | |
func webView(_: WKWebView, didFinish _: WKNavigation!) { | |
UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
UIView.transition(with: progressView, | |
duration: 0.33, | |
options: [.transitionCrossDissolve], | |
animations: { | |
self.progressView.isHidden = true | |
}, | |
completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment