Skip to content

Instantly share code, notes, and snippets.

@awunnenb
Last active October 14, 2024 23:19
Show Gist options
  • Select an option

  • Save awunnenb/dc0fcb4d221a0b5c42ea1ff315955458 to your computer and use it in GitHub Desktop.

Select an option

Save awunnenb/dc0fcb4d221a0b5c42ea1ff315955458 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)
}
}
@Andhar7
Copy link
Copy Markdown

Andhar7 commented Oct 2, 2021

Thank you so much...

@complabs28
Copy link
Copy Markdown

What version of Xcode and macOS are you using?

@awunnenb
Copy link
Copy Markdown
Author

awunnenb commented Oct 18, 2021

What version of Xcode and macOS are you using?

at the time of the video recording it was XCode 12:
https://youtu.be/SBvrvJ93gh4
https://youtu.be/o52XYvwTQU0

@awunnenb
Copy link
Copy Markdown
Author

What version of Xcode and macOS are you using?

it works with XCode 13 too

@complabs28
Copy link
Copy Markdown

What version of Xcode and macOS are you using?

it works with XCode 13 too

But When I use it with Xcode 13, It throws up an error:

  • Type of expression is ambiguous without more context
  • Invalid redeclaration of 'WebView'

Can you help me to fix this?

@awunnenb
Copy link
Copy Markdown
Author

Can you help me to fix this?

do you use the same code above?
You have to use WkWebView
private var webView: WKWebView?

@lolonolo
Copy link
Copy Markdown

Hi,
thank you, it's very nice.
but,
Some links do not work because <...target="_blank" ...> does not allow to open another page.
I found the following code from the internet.

but I need to add this: webView.uiDelegate = self
Cannot assign value of tyle "WebView" to type 'WKUIDelegate?'

"func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if let frame = navigationAction.targetFrame,
frame.isMainFrame {
return nil
}
webView.load(navigationAction.request)
return nil
}"

what I should do?

@awunnenb
Copy link
Copy Markdown
Author

check out https://youtu.be/o52XYvwTQU0 to handle external links please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment