Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

basiqio/basiq-ios-blink-demo

Repository files navigation

Basiq Blink iOS integration

Prerequisites

Installed Xcode version > 8.2 Installed Swift 3

Installation

Package manager is CocoaPods

sudo gem install cocoapods

pod install

Running

To open application in Xcode type from command-line

open demo.xcworkspace

Configuration variables which you should have set are the urls of the webview server, and your client server that will be responsible for creating a user and fetching the access token. You can check them in action here

Snippet from file:

var LocalServerHost = "http://0.0.0.0"
var BasiqWebViewEndpoint = "http://js.basiq.io/index.html"

Server requirements

To run the example, your API must expose two endpoints, /access_token and /user. These endpoints must return the access_token that was retrieved from Basiq's API with the CLIENT_CREDENTIALS scope, and a user_id that will be used to create the connection.

You can find an example server implementation here

Example WebViewClient

You can find the source code from the example here.

As you can see in the following code snippet:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .other{
            if let urlStr = navigationAction.request.mainDocumentURL?.absoluteURL.absoluteString.removingPercentEncoding!{
                var event = urlStr.substring(from: "basiq://".endIndex)
                if let lastIndex = event.range(of:"/"){
                    event = event.substring(to:(lastIndex.lowerBound))
                }
                switch event{
                case Constants.CONNECTION_EVENT:
                    let jsonvalue = urlStr.substring(from: "basiq://connection/".endIndex)
                    if let dataFromString = jsonvalue.data(using: .utf8) {
                        let json = try? JSON(data: dataFromString)
                        if let success = json?["success"], success.boolValue{
                            if let connectionId = json?["data"]["id"],connectionId.string != nil {
                                modelController.setConnectionID(connectionId: connectionId.string!)
                                self.dismiss(animated: true)
                                decisionHandler(.cancel)
                            }else{
                                modelController.setError(err: "Cannot parse connectionId from request data!")
                                self.dismiss(animated: true)
                                decisionHandler(.cancel)
                            }
                        }else{
                            //This should be handled on webview
                            print("Message should be disaplyed on web-view")
                            decisionHandler(.allow)
                        }
                    }else{
                        modelController.setError(err: "Cannot parse WebView request data!")
                        self.dismiss(animated: true)
                        decisionHandler(.cancel)
                    }
                case Constants.CANCELLATION_EVENT:
                    self.dismiss(animated: true)
                    decisionHandler(.cancel)
                default:
                    decisionHandler(.allow)
                }
            }else{
                modelController.setError(err: "Cannot parse WebView request URL!")
                self.dismiss(animated: true)
                decisionHandler(.cancel)
            }
        }else{
            //when user interacts with webview
            decisionHandler(.allow)
        }
    }

We detect if the url starts with the "basiq" protocol, and in that case parse it. The payload is a JSON string, and it comes after the event name. Example:

basiq://connection/{"id":"klqwd-qwdoijjoqd102wq-djkqw"}

In the example, connection is the event name, and the payload contains the connection id in the JSON. We can extract connectionId from such response, and continue to use it.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published