Skip to content

Commit

Permalink
Merge pull request #493 from tinkoff-mobile-tech/fix/EACQAPW-5350/hid…
Browse files Browse the repository at this point in the history
…e_json_contents_browser_3ds

* [EACQAPW-5350] Fixed showing JSON contents in WebView 3ds
  • Loading branch information
ivanglushko authored Jul 26, 2023
2 parents 798c9ef + 10d0c1f commit df9dd8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class ThreeDSWebViewController<Payload: Decodable>: UIViewController, WKNa
// MARK: Subviews

private lazy var webView = WKWebView()
private let hidingView = UIView()

// MARK: Init

Expand Down Expand Up @@ -68,6 +69,9 @@ final class ThreeDSWebViewController<Payload: Decodable>: 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
Expand All @@ -84,13 +88,27 @@ final class ThreeDSWebViewController<Payload: Decodable>: 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
Expand Down Expand Up @@ -142,3 +160,10 @@ final class ThreeDSWebViewController<Payload: Decodable>: 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"
}

0 comments on commit df9dd8f

Please sign in to comment.