Initializing the SDK

This document explains how to integrate the Dolby.io Communications Client SDKs into your application. If you use UI components, please refer to the UXKit.

Add the SDK to your application

To add the SDK to your application, you can either use Cocoapods, Carthage, Swift Package Manager, or install the SDK manually. The full instructions are available in the Installing the SDK document.

Initialize the SDK

The iOS Client SDK provides the initialize method to authenticate against the service using an access token. For more information, see the section titled Initialize the SDK with secure authentication.

When the application has received the access token from the customer’s server, it can initialize the SDK with:

import VoxeetSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func fetchToken(completion: @escaping (_ token: String?) -> Void) {
        let url = URL(string: serverURL + "/api/token")!
        let task = URLSession.shared.dataTask(with: url) { data, _, _ in
            if let data = data,
              let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
              let accessToken = json["access_token"] as? String {
                completion(accessToken)
            } else {
                completion(nil)
            }
        }
        task.resume()
    }

    func application(
        application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {

        // Fetch access token.
        fetchToken { accessToken in
            guard let token = accessToken else { return }

            // Voxeet SDK OAuth initialization.
            VoxeetSDK.shared.initialize(accessToken: token) { refreshClosure in
                // VoxeetSDK calls this closure when the token needs to be refreshed.
                // See the next section for details.
            }
        }

        return true
    }
}

Refresh authentication

An access token has a limited period of validity and needs periodic refreshing. In the application, the Dolby.io Communications Client SDKs invokes the callback provided to the initialize call when the access token needs to be refreshed. This callback contacts the customer’s server, which in turn calls the /oauth2/token API again and returns the refreshed token, which is passed back to the Client SDK by the application.

When the application has received the refreshed access token from the customer’s server, it can pass this to the SDK with this change:

// The last parameter passed to `VoxeetSDK.initialize(accessToken:refreshTokenClosure:)` 
// is a closure that is called when the token needs to be refreshed. This 
// closure is passed a single parameter, which is a `VoxeetSDK` closure, which 
// the application calls with the refreshed access token

import VoxeetSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    ...

    func application(
        application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {

        // Fetch access token.
        fetchToken { accessToken in
            guard let token = accessToken else { return }

            // Voxeet SDK OAuth initialization.
            VoxeetSDK.shared.initialize(accessToken: token) { refreshClosure in
                // VoxeetSDK calls this closure when the token needs to be refreshed.
                fetchToken { accessToken in
                    guard let token = accessToken else { return }

                    // Call the SDK’s refresh closure with the new token
                    refreshClosure(token)
                }
            }
        }

        return true
    }
}

Close a session

VoxeetSDK.shared.session.close { error in 
}

Result

The Dolby.io Communications Client SDKs is integrated with your application, so you have access to all SDK functionalities.

Now you can easily configure your SDK. Tutorials available in the left-side navigation panel and reference documentation guides how to do it.


Did this page help you?