Repository navigation
Expand file tree
/
Copy pathWebViewExampleViewController.swift
More file actions
168 lines (136 loc) · 5.31 KB
/
Copy pathWebViewExampleViewController.swift
File metadata and controls
168 lines (136 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// WebViewExampleViewController.swift
// GradientProgressBarExample
//
// Created by Felix Mau on 02.02.22.
// Copyright © 2022 Felix Mau. All rights reserved.
//
import GradientProgressBar
import SwiftUI
import UIKit
import WebKit
/// A view controller demonstrating the usage of a `GradientProgressBar`
/// to reflect the loading progress of a `WKWebView`.
///
/// Based on: <https://gist.github.com/fxm90/50d6c73d07c4d9755981b9bb4c5ab931>
final class WebViewExampleViewController: UIViewController {
// MARK: - Config
private enum Config {
/// The initial URL to load in the web view.
static let initialUrl = URL(string: "https://felix.hamburg")!
/// The duration of the fade-in/-out animation of the progress view.
static let fadeDuration: TimeInterval = 0.33
}
// MARK: - Private Properties
/// The web view from the interface builder.
private let webView = WKWebView()
/// Progress view reflecting the current loading progress of the web view.
private let progressView = GradientProgressBar()
/// The observation object for the progress of the web view.
private var estimatedProgressObserver: NSKeyValueObservation?
/// The navigation bar of the current navigation controller.
/// Using a computed property here simplifies handling the optional value.
private var navigationBar: UINavigationBar {
guard let navigationBar = navigationController?.navigationBar else {
fatalError("⚠️ – Expected view controller to be embedded in a navigation controller.")
}
return navigationBar
}
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setUpNavigationBar()
setUpProgressView()
setUpWebView()
setUpConstraints()
}
override func viewLayoutMarginsDidChange() {
// Workaround to prevent the navigation bar’s large title from collapsing when the WebView finishes loading.
// Based on: https://stackoverflow.com/a/68573703
guard !webView.isLoading else { return }
navigationBar.sizeToFit()
}
// MARK: - Private Methods
private func setUpNavigationBar() {
navigationBar.prefersLargeTitles = true
navigationItem.title = "🌍 WebView Example"
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: self,
action: #selector(refreshButtonTouchUpInside(_:)),
)
}
@objc
private func refreshButtonTouchUpInside(_: Any) {
webView.reload()
}
private func setUpProgressView() {
progressView.isHidden = true
progressView.translatesAutoresizingMaskIntoConstraints = false
navigationBar.addSubview(progressView)
}
private func setUpWebView() {
webView.navigationDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
// Monitor the estimated progress of the page load using KVO.
//
// The KVO change handler is inferred as a `@Sendable` closure and is not guaranteed to execute on the main actor.
// Since `progress` is main-actor isolated, we explicitly switch to the main actor using an unstructured `Task` before
// mutating any UI state.
estimatedProgressObserver = webView.observe(\.estimatedProgress, options: [.new]) { [weak self] webView, _ in
Task { @MainActor in
self?.progressView.progress = Float(webView.estimatedProgress)
}
}
let request = URLRequest(url: Config.initialUrl)
webView.load(request)
}
private func setUpConstraints() {
NSLayoutConstraint.activate([
progressView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor),
progressView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor),
progressView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor),
progressView.heightAnchor.constraint(equalToConstant: 3),
webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
webView.topAnchor.constraint(equalTo: view.topAnchor),
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
}
// MARK: - `WKNavigationDelegate` conformance
/// By implementing the `WKNavigationDelegate` we can update the visibility of the `progressView`
/// according to the `WKNavigation` navigation progress.
///
/// Based on: <https://gist.github.com/fxm90/723b5def31b46035cd92a641e3b184f6>
extension WebViewExampleViewController: WKNavigationDelegate {
func webView(_: WKWebView, didStartProvisionalNavigation _: WKNavigation!) {
if progressView.isHidden {
// Make sure our animation is visible.
progressView.isHidden = false
}
UIView.animate(
withDuration: Config.fadeDuration,
animations: { self.progressView.alpha = 1 },
)
}
func webView(_: WKWebView, didFinish _: WKNavigation!) {
UIView.animate(
withDuration: Config.fadeDuration,
animations: { self.progressView.alpha = 0 },
completion: { isFinished in
// Update `isHidden` flag accordingly:
// - set to `true` in case animation was completely finished.
// - set to `false` in case animation was interrupted, e.g. due to starting of another animation.
self.progressView.isHidden = isFinished
},
)
}
}
// MARK: - Preview
#Preview {
UINavigationController(
rootViewController: WebViewExampleViewController(),
)
}