A WKWebView
wrapper for SwiftUI
with Javascript interoperability.
- Create
SwiftUIWithJSInteropWebViewModel
, specify message handlers and the webview's URL.
let url = URL(string: "your_webview_url")!
let viewModel = SwiftUIWithJSInteropWebViewModel(url: url, messageHandlerNames: ["yourMessageHander"])
- Include
SwiftUIWithJSInteropWebView
along withSwiftUIWithJSInteropWebViewModel
in your SwiftUI view.
struct ContentView: View {
var body: some View {
SwiftUIWithJSInteropWebView(viewModel: viewModel, navigationDelegate: nil)
}
}
-
Assign object that conforms
SwiftUIWithJSInteropWebViewModelDelegate
asSwiftUIWithJSInteropWebViewModel
's delegate to handle messages from the webview. -
Evaluate javascript and load another URL into the webview via
SwiftUIWithJSInteropWebViewModel
asWebviewInteropInterface
.
Check demo app project SwiftUIWebviewWithJSInteropDemo
for complete code example.