-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathWebViewTestView.swift
More file actions
174 lines (151 loc) 路 5.74 KB
/
Copy pathWebViewTestView.swift
File metadata and controls
174 lines (151 loc) 路 5.74 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
169
170
171
172
173
174
//
// WebViewTestView.swift
// Example
//
// Created by Matheus Gois.
//
import SwiftUI
import WebKit
struct WebViewTestView: View {
@State private var isLoading = true
@State private var canGoBack = false
@State private var canGoForward = false
@State private var currentURL = "https://www.google.com"
var body: some View {
NavigationView {
VStack(spacing: 0) {
// URL Bar
HStack {
HStack {
Image(systemName: "lock.shield")
.foregroundColor(.green)
.font(.caption)
Text(currentURL)
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(1)
.truncationMode(.middle)
Spacer()
if isLoading {
ProgressView()
.scaleEffect(0.7)
} else {
Button(action: {
// Refresh action will be handled by WebView
}) {
Image(systemName: "arrow.clockwise")
.font(.caption)
}
}
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(Color(.systemGray6))
.cornerRadius(8)
}
.padding(.horizontal)
.padding(.top, 8)
// Navigation Controls
HStack(spacing: 20) {
Button(action: {
// Go back action will be handled by WebView
}) {
Image(systemName: "chevron.left")
.font(.title2)
.foregroundColor(canGoBack ? .blue : .gray)
}
.disabled(!canGoBack)
Button(action: {
// Go forward action will be handled by WebView
}) {
Image(systemName: "chevron.right")
.font(.title2)
.foregroundColor(canGoForward ? .blue : .gray)
}
.disabled(!canGoForward)
Spacer()
Button(action: {
// Share action
}) {
Image(systemName: "square.and.arrow.up")
.font(.title2)
.foregroundColor(.blue)
}
}
.padding(.horizontal, 20)
.padding(.vertical, 12)
.background(Color(.systemBackground))
.overlay(
Rectangle()
.frame(height: 0.5)
.foregroundColor(Color(.separator)),
alignment: .bottom
)
// WebView
GoogleWebView(
isLoading: $isLoading,
canGoBack: $canGoBack,
canGoForward: $canGoForward,
currentURL: $currentURL
)
.edgesIgnoringSafeArea(.bottom)
}
}
.navigationBarTitle("Google WebView", displayMode: .inline)
.navigationBarTitleDisplayMode(.inline)
}
}
struct GoogleWebView: UIViewRepresentable {
@Binding var isLoading: Bool
@Binding var canGoBack: Bool
@Binding var canGoForward: Bool
@Binding var currentURL: String
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.navigationDelegate = context.coordinator
// Load Google
if let url = URL(string: "https://www.google.com") {
let request = URLRequest(url: url)
webView.load(request)
}
return webView
}
func updateUIView(_ webView: WKWebView, context: Context) {
// Update navigation state
DispatchQueue.main.async {
self.canGoBack = webView.canGoBack
self.canGoForward = webView.canGoForward
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate {
let parent: GoogleWebView
init(_ parent: GoogleWebView) {
self.parent = parent
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
DispatchQueue.main.async {
self.parent.isLoading = true
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.async {
self.parent.isLoading = false
self.parent.canGoBack = webView.canGoBack
self.parent.canGoForward = webView.canGoForward
self.parent.currentURL = webView.url?.absoluteString ?? ""
}
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
DispatchQueue.main.async {
self.parent.isLoading = false
}
print("WebView navigation failed: \(error.localizedDescription)")
}
}
}
#Preview {
WebViewTestView()
}