From 10d0c1f36593ca5bda50a556bb14c5e09a8bca14 Mon Sep 17 00:00:00 2001 From: Ivan Glushko Date: Wed, 26 Jul 2023 15:45:40 +0400 Subject: [PATCH] * [EACQAPW-5350] Fixed showing JSON contents in WebView 3ds --- CHANGELOG.md | 1 + .../ThreeDSWebViewController.swift | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 304ccf46d..72cd936bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ * [EACQAPW-4285] Remove LOOP_CHECKING status in card attachment * [EACQAPW-5657] Pay by Card email field bugfixes +* [EACQAPW-5350] Fixed showing JSON contents in WebView 3ds ## [3.1.1] - 2023-07-17Z diff --git a/TinkoffASDKUI/TinkoffASDKUI/BusinessLogic/CommonServcies/WebBasedThreeDS/ThreeDSWebView/ThreeDSWebViewController.swift b/TinkoffASDKUI/TinkoffASDKUI/BusinessLogic/CommonServcies/WebBasedThreeDS/ThreeDSWebView/ThreeDSWebViewController.swift index 2c57c93ef..30da5d980 100644 --- a/TinkoffASDKUI/TinkoffASDKUI/BusinessLogic/CommonServcies/WebBasedThreeDS/ThreeDSWebView/ThreeDSWebViewController.swift +++ b/TinkoffASDKUI/TinkoffASDKUI/BusinessLogic/CommonServcies/WebBasedThreeDS/ThreeDSWebView/ThreeDSWebViewController.swift @@ -32,6 +32,7 @@ final class ThreeDSWebViewController: UIViewController, WKNa // MARK: Subviews private lazy var webView = WKWebView() + private let hidingView = UIView() // MARK: Init @@ -68,6 +69,9 @@ final class ThreeDSWebViewController: UIViewController, WKNa override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) webView.load(urlRequest) + hidingView.frame = webView.frame + hidingView.backgroundColor = ASDKColors.Background.base.color + hidingView.isHidden = true } // MARK: WKNavigationDelegate @@ -84,13 +88,27 @@ final class ThreeDSWebViewController: UIViewController, WKNa ) } + func webView( + _ webView: WKWebView, + decidePolicyFor navigationResponse: WKNavigationResponse, + decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void + ) { + let isShowingJsonMimeType = navigationResponse.response.mimeType == .jsonMimeType + hidingView.isHidden = !isShowingJsonMimeType + hidingView.removeFromSuperview() + if isShowingJsonMimeType { + webView.addSubview(hidingView) + } + decisionHandler(.allow) + } + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { - webView.evaluateJavaScript("document.baseURI") { [weak self] value, error in + webView.evaluateJavaScript(.baseURI) { [weak self] value, error in guard error == nil, let uri = value as? String else { return } - webView.evaluateJavaScript("document.getElementsByTagName('pre')[0].innerText") { value, _ in + webView.evaluateJavaScript(.jsonText) { value, _ in guard let response = value as? String, let responseData = response.data(using: .utf8), let self = self @@ -142,3 +160,10 @@ final class ThreeDSWebViewController: UIViewController, WKNa } } } + +private extension String { + // Javascript code snippets + static let baseURI = "document.baseURI" + static let jsonText = "document.getElementsByTagName('pre')[0].innerText" + static let jsonMimeType = "application/json" +}