Last updated

iOS (swift)

Prerequisites

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

  • Can create access tokens in your server-side.
  • Know how to implement webviews for your platform. For more information, see the webview articles from Apple.

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

Deeplink Configuration Example
<key>CFBundleURLTypes</key>
<array>
   <dict>
      <key>CFBundleURLName</key>
      <string>[YOUR IDENTIFIER]</string> <!-- Your app name -->
      <key>CFBundleURLSchemes</key>
      <array>
         <string>your-url-here</string> <!-- Your deeplink URL -->
      </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.

Webview Handling Example
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:

PAGE_LOAD Event
{
    "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
    }
}

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.configuration.userContentController.add(self, name: "belvoWidget") // Used to listen for additional events

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