Last active
April 29, 2020 16:45
-
-
Save prafullakumar/1c1ecafe1328b4b689d795b2f9a6748d 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 { | |
... | |
final class Coordinator: NSObject { | |
@ObservedObject var webViewStateModel: WebViewStateModel | |
let action: ((_ navigationAction: WebView.NavigationAction) -> Void)? | |
init(action: ((_ navigationAction: WebView.NavigationAction) -> Void)?, | |
webViewStateModel: WebViewStateModel) { | |
self.action = action | |
self.webViewStateModel = webViewStateModel | |
} | |
} | |
} | |
extension WebViewWrapper.Coordinator: WKNavigationDelegate { | |
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { | |
if action == nil { | |
decisionHandler(.allow) //if no custom delegates always return allow | |
} else { | |
action?(.decidePolicy(navigationAction, decisionHandler)) | |
} | |
} | |
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { | |
webViewStateModel.loading = true | |
action?(.didStartProvisionalNavigation(navigation)) | |
} | |
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) { | |
action?(.didReceiveServerRedirectForProvisionalNavigation(navigation)) | |
} | |
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { | |
webViewStateModel.loading = false | |
webViewStateModel.canGoBack = webView.canGoBack | |
action?(.didFailProvisionalNavigation(navigation, error)) | |
} | |
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { | |
action?(.didCommit(navigation)) | |
} | |
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | |
webViewStateModel.loading = false | |
webViewStateModel.canGoBack = webView.canGoBack | |
if let title = webView.title { | |
webViewStateModel.pageTitle = title | |
} | |
action?(.didFinish(navigation)) | |
} | |
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { | |
webViewStateModel.loading = false | |
webViewStateModel.canGoBack = webView.canGoBack | |
action?(.didFail(navigation, error)) | |
} | |
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { | |
if action == nil { | |
completionHandler(.performDefaultHandling, nil) | |
} else { | |
action?(.didRecieveAuthChallange(challenge, completionHandler)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment