Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Last active April 29, 2020 16:48
Show Gist options
  • Save prafullakumar/e0e3aab5b7a5e6dc5745a2d27f7cfc9f to your computer and use it in GitHub Desktop.
Save prafullakumar/e0e3aab5b7a5e6dc5745a2d27f7cfc9f to your computer and use it in GitHub Desktop.
final class WebViewWrapper : UIViewRepresentable {
@ObservedObject var webViewStateModel: WebViewStateModel //action two way binding
let action: ((_ navigationAction: WebView.NavigationAction) -> Void)? //delegates callback
let request: URLRequest
init(webViewStateModel: WebViewStateModel,
action: ((_ navigationAction: WebView.NavigationAction) -> Void)?,
request: URLRequest) {
self.action = action
self.request = request
self.webViewStateModel = webViewStateModel
}
func makeUIView(context: Context) -> WKWebView {
let view = WKWebView()
view.navigationDelegate = context.coordinator
view.load(request)
return view
}
func updateUIView(_ uiView: WKWebView, context: Context) {
if uiView.canGoBack, webViewStateModel.goBack {
uiView.goBack() //go back if user tapped go back and last page is available
webViewStateModel.goBack = false
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(action: action, webViewStateModel: webViewStateModel)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment