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

Please note that when rendering the Trustly Checkout from a native app you are required to pass your application's URLScheme as an attribute to the order initiation request. 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.
  • It's important that the "URLScheme" attribute is only sent if the order is triggered by users from within the app.
  • For any other transaction instances (ex. desktop or interaction via mobile browser directly) the "URLScheme" attribute must not be included.
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

🚧

NOTE: The latest version of the SDK does not support the older version of the Trustly Checkout. If you do use the older Checkout, please use version 2.0.0 of the SDK. If you're not sure what version of the Trustly Checkout you are using, please contact our integration support for help

  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?) {

  }
}