Skip to content

Instantly share code, notes, and snippets.

@seanlilmateus
Forked from awunnenb/ContentView.swift
Created June 24, 2022 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanlilmateus/350c74c0dd25d52b02787d72fd7fce54 to your computer and use it in GitHub Desktop.
Save seanlilmateus/350c74c0dd25d52b02787d72fd7fce54 to your computer and use it in GitHub Desktop.
SwiftUI WKWebView and Back- Forward Buttons
// Youtube Video: https://youtu.be/SBvrvJ93gh4
import SwiftUI
import WebKit
struct ContentView: View {
let webView = WebView(request: URLRequest(url: URL(string: "https://www.google.com")!))
var body: some View {
VStack {
webView
HStack {
Button(action: {
self.webView.goBack()
}){
Image(systemName: "arrowtriangle.left.fill")
.font(.title)
.foregroundColor(.blue)
.padding()
}
Spacer()
Button(action: {
self.webView.goHome()
}){
Image(systemName: "house.fill")
.font(.title)
.foregroundColor(.blue)
.padding()
}
Spacer()
Button(action: {
self.webView.refresh()
}){
Image(systemName: "arrow.clockwise.circle.fill")
.font(.title)
.foregroundColor(.blue)
.padding()
}
Spacer()
Button(action: {
self.webView.goForward()
}){
Image(systemName: "arrowtriangle.right.fill")
.font(.title)
.foregroundColor(.blue)
.padding()
}
}
}
}
}
struct WebView: UIViewRepresentable {
let request: URLRequest
private var webView: WKWebView?
init(request: URLRequest) {
self.webView = WKWebView()
self.request = request
}
func makeUIView(context: Context) -> WKWebView {
return webView!
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}
func goBack(){
webView?.goBack()
}
func goForward(){
webView?.goForward()
}
func refresh() {
webView?.reload()
}
func goHome() {
webView?.load(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment