Error state in URL bar

This commit is contained in:
James Magahern
2020-07-31 18:29:44 -07:00
parent 2a64636d61
commit 29c416374f
2 changed files with 100 additions and 7 deletions

View File

@@ -28,6 +28,8 @@ class BrowserViewController: UIViewController, WKNavigationDelegate,
private var backButtonObservation: NSKeyValueObservation?
private var forwardButtonObservation: NSKeyValueObservation?
private var loadError: Error?
override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent }
init() {
@@ -139,6 +141,22 @@ class BrowserViewController: UIViewController, WKNavigationDelegate,
// New tab button
toolbarController.newTabButton.addAction(newTabAction, for: .touchUpInside)
// Error button
toolbarController.urlBar.errorButton.addAction(UIAction(handler: { [unowned self] _ in
let alert = UIAlertController(title: "Error", message: self.loadError?.localizedDescription, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Reload", style: .destructive, handler: { _ in
self.webView.reload()
alert.dismiss(animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}), for: .touchUpInside)
// TextField delegate
toolbarController.urlBar.textField.delegate = self
@@ -146,7 +164,9 @@ class BrowserViewController: UIViewController, WKNavigationDelegate,
}
private func updateLoadProgress(forWebView webView: WKWebView) {
if webView.estimatedProgress == 1.0 {
if let loadError = loadError {
toolbarController.urlBar.loadProgress = .error(error: loadError)
} else if webView.estimatedProgress == 1.0 {
toolbarController.urlBar.loadProgress = .complete
} else {
toolbarController.urlBar.loadProgress = .loading(progress: webView.estimatedProgress)
@@ -243,6 +263,8 @@ class BrowserViewController: UIViewController, WKNavigationDelegate,
// MARK: Navigation Delegate
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
loadError = nil
// Reset tracking this
tab.allowedScriptOrigins.removeAll()
tab.blockedScriptOrigins.removeAll()
@@ -272,6 +294,14 @@ class BrowserViewController: UIViewController, WKNavigationDelegate,
decisionHandler(.allow, preferences)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
self.loadError = error
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
self.loadError = error
}
// MARK: UITextField Delegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {