Last active
April 29, 2020 16:48
-
-
Save prafullakumar/e0e3aab5b7a5e6dc5745a2d27f7cfc9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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