-
-
Save awunnenb/dc0fcb4d221a0b5c42ea1ff315955458 to your computer and use it in GitHub Desktop.
// 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) | |
} | |
} | |
Can you help me to fix this?
do you use the same code above?
You have to use WkWebView
private var webView: WKWebView?
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?
check out https://youtu.be/o52XYvwTQU0 to handle external links please
But When I use it with Xcode 13, It throws up an error:
Can you help me to fix this?