iOS (Swift)

Check out some examples of how to set up deeplinks and listen for webview redirects in iOS 🍏.

Prerequisites

In order to be able to use the hosted widget in your iOS app, make sure that you:

Set up deeplinks in your iOS application

In your Info.plist file, add the following code:

<key>CFBundleURLTypes</key>
<array>
   <dict>
      <key>CFBundleURLName</key>
      <string>[YOUR IDENTIFIER]</string> <!-- Your app name -->
      <key>CFBundleURLSchemes</key>
      <array>
         <string>belvowidget</string> <!-- The Belvo deeplink you need to add. -->
      </array>
   </dict>
</array>

Handle events in your webview

In the code sample below you can see an example of how to listen and handle for events within your webview.

public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
        if(navigationAction.navigationType == .other) {
            if navigationAction.request.url != nil {
                if navigationAction.request.url.host == "success" {
                    var url = navigationAction.request.url
                    var link = self.getParameterFrom(url: url, param: "link")
                    var institution = self.getParameterFrom(url: url, param: "institution")
                  // Do something with the link and institution.
                }
                else if navigationAction.request.url.host == "error" {
                    // If the redirect starts with "error",
                    // Do something with the error.
                } 
                else {
                  // If the redirect starts with "exit",
                  // Do something with the exit information.
                }
            }
            decisionHandler(.cancel)
            return
        }
        decisionHandler(.allow)
    }
self.webView.configuration.userContentController.add(self, name: "belvoWidget") // Used to listen for additional events

Listening to additional events

Our widget for webviews also sends additional data regarding events the user encounters throughout the widget. For example, when the user goes from the institution selection screen to the credentials login screen, our widget will send an event.

The events are sent through as JSON payloads with the following schema:

"data": {
    "eventName": "PAGE_LOAD",
    "metadata":{
        "page": "/institutions", // Page that the user is directed to
        "from": "/consent", // Page where the user was previously
        "institution_name": "", // Note: This field only appears AFTER they've selected an institution
    }
}
data = {
  "eventName": "ERROR",
  "request_id": "UUID",
  "meta_data": {
    "error_code": "login_error",
    "error_message": "Invalid credentials provided to login to the institution",
    "institution_name": "bancoazteca_mx_retail",
    "timestamp": "2020-04-27T19:09:23.918Z"
  }
}

To listen to these events:

  1. Add the following protocol to your ViewController.
WKScriptMessageHandler
  1. Add the following method to your application:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
    let body = "EVENT DATA: " + (message.body as! String)
    print( body as Any )
        
}

  1. Add the following line of code to the end of your logic for handling deep links in your webview:
belvoWebView.addJavascriptInterface(BelvoWidget(), "belvoWidget") // Used to listen for additional events

✅ Done! You can now listen to additional events in your webview!