Hi, I'm relatively new with WebKit and need to work with it in order to display HTML in my App, but I want links that users tap on inside the WebView to open in safari or an external browser.
I've seen a bunch of solutions using view controllers and delegates, but I desperately need a simple solution for SwiftUI.
import WebKit
struct HTMLView: UIViewRepresentable {
let htmlString : String
func makeUIView(context: Context) -> WKWebView {
let view = WKWebView(frame: .zero)
view.allowsLinkPreview = true
return view
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(htmlString, baseURL: nil)
}
}
Implementation :
struct ContentView: View {
var body: some View {
HTMLView(htmlString: "<p>We are apple <a href='https://developer.apple.com/'> Developers</a></p>")
}
}
Here's what I'm working with, Please help.