Trustly iOS SDK

Trustly fully supports native iOS apps. On this page, you will find instructions on how-to configure Trustly in iOS apps.

Trustly iOS SDK has been created for iOS to simplify the process of implementing the functionality needed when using Trustly within an iOS app. It provides a self-contained WKWebView that enables the Trustly Checkout within your application.

CUSTOM URL SCHEME

To properly integrate Trustly’s SDK, it’s required to use a custom URL scheme for your iOS app. By doing so, Trustly can redirect users back to your app after using external identification apps such as Mobile BankID.

It's a straightforward process; you can follow any Tutorial on iOS/iPhone Custom URL Schemes. The URL scheme can be registered using the following method:

  • Include it in the "URLScheme" attribute when making an API call to Trustly.
Attribute nameDescriptionTypeExample
URLSchemeThe URL scheme used to redirect users back to your iOS appTextyourCustomURLScheme://

Example:

{
    "method": "Deposit",
    "params": {
        "Signature": "f4ThjuMqbsdG6u ... S16VbzD4h==",
        "UUID": "258a2184-2842-b485-25ca-293525152425",
        "Data": { 
            "Username": "merchant_username",
            "Password": "merchant_password",
            "Attributes": {
                "Country": "SE",
                "Locale": "sv_SE",
                "URLScheme":"myapp://"
                ...

Configure Trustly iOS SDK:

Import Swift Package

  1. Navigate to File -> Swift Packages -> Add Package Dependency..
  1. Paste the Trustly SDK URL: https://github.com/trustly/TrustlyIosSdk
  1. Select Up to Next Major version and make sure you have the latest version
  1. Press finish.
  1. You should now see the swift package in the project navigator.

Modify ViewController

Add below imports to your ViewController.swift:

import UIKit
import WebKit
import TrustlyIosSdk

Add the following code to your ViewController.swift:

class ViewController: UIViewController, WKNavigationDelegate{

    var webView: WKWebView!
    override func viewDidLoad() {

        super.viewDidLoad()

        let mainView = TrustlyWKWebView(checkoutUrl:
        "< Insert Trustly Url >", frame: self.view.bounds)

        self.view = mainView
    }
}

Code example

This is an example configuration of the ViewController.swift that renders the Trustly checkout in a webview:

import UIKit
import WebKit
import TrustlyIosSdk

class ViewController: UIViewController, WKNavigationDelegate{

    var webView: WKWebView!
    override func viewDidLoad() {

        super.viewDidLoad()

        let mainView = TrustlyWKWebView(checkoutUrl: 
        "< Insert Trustly Url >", frame: self.view.bounds)

        self.view = mainView
    }
}

Receiving Checkout Events

If you want more control of your Checkout flow you can choose to opt-in to receiving and handling Checkout events.

You can opt-in by setting the delegate of the TrustlyWKWebView

trustlyWebView?.delegate = self

and conforming to the TrustlyCheckoutDelegate protocol

class ViewController: UIViewController, TrustlyCheckoutDelegate {

  func onTrustlyCheckoutRequstToOpenURLScheme(urlScheme: String) {
    //Requests to open URLs or third party applications.
  }

  func onTrustlyCheckoutSuccessfull(urlString: String?) {

  }

  func onTrustlyCheckoutError() {

  }

  func onTrustlyCheckoutAbort(urlString: String?) {

  }
}