WKWebView

In this section, you will find how to implement the webview on iOS using 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 IDPay), 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()
    }
}

Example of how it should look in the app:

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

  • Camera

  • Geolocation

To learn more, we recommend reading the following articles and documentation:

  • To access the official documentation, click here.


Still need help?

Didn't find something or still need help? If you're already a client or partner, you can reach out through our Help Center.

Last updated

Copyright © 2024 unico. All rights reserved.