# 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. ## Set up deeplinks in your iOS application In your `Info.plist` file, add the following code: ```xml Deeplink Configuration Example CFBundleURLTypes CFBundleURLName [YOUR IDENTIFIER] CFBundleURLSchemes your-url-here ``` ## 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. ```swift 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 ```json 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 } } ``` Error Event ```json ERROR Event { "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. ```swift WKScriptMessageHandler ``` 1. Add the following method to your application: ```swift 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: ```kotlin belvoWebview.configuration.userContentController.add(self, name: "belvoWidget") // Used to listen for additional events ``` Done! You can now listen to additional events in your webview!