Skip to content

* [EACQAPW-5350] Fixed showing JSON contents in WebView 3ds #493

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
Loading