Skip to main content

WKWebView

For the iOS usage scenario, using WKWebView is one of the recommended approaches.

After creating the transaction and obtaining the transaction link, the following implementation is recommended:

  • In your common flow (which includes Card Not Present Verification), you will open the WKWebView with the link generated via the API;
  • You can customize this opening as needed for your app;
  • You will monitor if the URL has changed (to the redirectUrl) and then close the WKWebView.

Opening a WKWebView is quite simple:

import UIKit
import WebKit
import SafariServices

var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = .white
}

@IBAction func open(_ sender: Any) {
createWKWebViewFull()
}

func createWKWebViewFull() {
webView = WKWebView(frame: self.view.bounds, configuration: getWKWebViewConfiguration())
webView.navigationDelegate = self
view.addSubview(webView)
self.loadUrl()
}

func loadUrl() {
if let url = URL(string:"<URL_TO_LOAD>") {
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}

private func getWKWebViewConfiguration() -> WKWebViewConfiguration {
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
config.mediaTypesRequiringUserActionForPlayback = []

return config
}

To control when to close the WKWebView, this can be done as follows:

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
if let currentURL = webView.url {
print("URL changed to: \(currentURL.absoluteString)")
if currentURL.absoluteString == "<URL_TO_OBSERVER>" {
closeWebView()
}
}
}

@objc func closeWebView() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.webView.removeFromSuperview()
}
}
note

It is necessary to have some permissions to function correctly, such as:

  • Camera
  • Geolocation

To learn more, we recommend reading the official WKWebView documentation.