# 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:&#x20;

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

Opening a WKWebView is quite simple:

```javascript
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:

```javascript
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:

<figure><img src="/files/d0J4BkjCVYJ8P3c0aImW" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
It is necessary to have some permissions to function correctly, such as:

* Camera
* Geolocation
  {% endhint %}

To learn more, we recommend reading the following articles and documentation:&#x20;

* To access the official documentation, click [<mark style="color:blue;">**here**</mark>](https://developer.apple.com/documentation/webkit/wkwebview).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devcenter.unico.io/unico-idpay/en/integration/controlling-the-experience/mobile/ios/wkwebview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
