Calls SDKs iOS v1
Calls SDKs iOS
Calls SDKs
iOS
Version 1

Authentication

Copy link

In order to use the features of the Calls SDK in your apps, user authentication with Sendbird server must be initiated through the authenticate() method. To receive direct calls, a user’s device token must be registered to the server. A device token can be registered by using the registerVoIPPush() or registerRemotePush() method after a user’s authentication has been completed.

Note: Even if your organization already uses Sendbird Chat, a separate user authentication is needed for Sendbird Calls.


Initialize the Calls SDK with APP_ID

Copy link

As shown below, the SendBirdCall instance must be initialized when a client app is launched. Initialization is done by using the APP_ID of your Sendbird application in the Dashboard. If the instance is initialized with a different APP_ID, all existing call-related data in a client app will be cleared and the SendBirdCall instance will be initialized again with the new APP_ID.

// Initialize SendBirdCall instance to use APIs in your app.
SendBirdCall.configure(appId: APP_ID)

Authenticate to Sendbird server

Copy link

In order to use the interfaces and methods provided in the SendBirdCall instance, a user must be connected to Sendbird server by authentication. Without authentication, calling the methods of the Calls SDK will not work in a user’s client app. This means that if a user performs actions such as accepting an incoming call or entering a room before authentication, the user will receive errors from Sendbird server. Also, any other events won’t be received through the Calls SDK if the user isn’t authenticated, except for the incoming calls through push notifications.

For example, the SendBirdCall.authenticate(with:) method should be called before calling the call.accept() in the didFinishLaunchingWithOptions method of your app’s AppDelegate as shown below:

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        SendBirdCall.configure(YOUR_APP_ID)

        // The USER_ID below should be unique to your Sendbird application.
        let params = AuthenticateParams(userId: USER_ID, accessToken: ACCESS_TOKEN)
        SendBirdCall.authenticate(with: params) { (user, error) in
            guard let user = user, error == nil else {
                // Handle error.
            }

            // The user has been authenticated successfully and is connected to Sendbird server.
            // Register device token by using the `SendBirdCall.registerVoIPPush` or `SendBirdCall.registerRemotePush` methods.
            ...
        }
    }
}

It is recommended to save the current user’s ID to the UserDefaults when a client app launches so that immediate authentication can take place by using the saved user ID.

While using CallKit, if the call.decline() is called before user authentication is completed with Sendbird server, a user will receive errors from the server and the CallKit view might hang without response. There are two ways to solve this problem:

1. Call the fulfill() method after user authentication

Copy link

The fulfill() method should be called within the callback of the SendBirdCall.authenticate() method after the call.decline() is called.

// Authentication
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
    guard let call = SendBirdCall.getCall(forUUID: action.callUUID) else {
        action.fail()
        return
    }

    let params = AuthenticateParams(userId: USER_ID, accessToken: ACCESS_TOKEN)
    SendBirdCall.authenticate(with: params) { (user, error) in
        call.decline()
        action.fulfill()
    }
}

2. Use background modes

Copy link

Enable background modes by using the beginBackgroundTask() method and call the fulfill() method at the end. Then the Calls SDK will have enough time in the background to complete user authentication and decline an incoming call. This will prevent a client app from being immediately terminated.

func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
    guard let call = SendBirdCall.getCall(forUUID: action.callUUID) else {
        action.fail()
        return
    }
    self.taskId = UIApplication.shared.beginBackgroundTask {
        UIApplication.shared.endBackgroundTask(self.taskId)
    }

    let params = AuthenticateParams(userId: USER_ID, accessToken: ACCESS_TOKEN)
    SendBirdCall.authenticate(with: params) { (user, error) in
        guard let user = user, error == nil else {
            // Handle error.
        }

        call.decline()
    }
    action.fulfill()
}

3. WebSocket

Copy link

WebSocket connections with Sendbird server are made only when there is an ongoing direct call or group call, therefore authenticating won't create unnecessary WebSocket connections. Also, unless you have integrated push notifications to your application, new incoming calls won't be delivered through WebSocket. The Calls SDK versions prior to 1.6.0 supported making calls with WebSocket connections under certain circumstances, however it is now deprecated.

Note: Sendbird Calls is charged by minutes which starts as soon as the call is connected. It is recommended that you authenticate users for the Calls SDK at your earliest convenience.

Using a session token

Copy link

You can also use a session token instead of an access token to authenticate a user. Session tokens are a more secure option because they expire after a certain period whereas access tokens don't. See Chat Platform API guides for further explanation about the difference between access token and session token, how to issue a session token, and how to revoke all session tokens.


Deauthenticate from Sendbird server

Copy link

Using the SendBirdCall.deauthenticate(completionHandler:) method, deauthenticate a user from Sendbird server. It will clear all current direct calls, session keys, and user information from a user’s client app. This can also be considered as logging out from the server.

Note: To stop receiving push notifications after deauthentication, unregister device tokens by using the SendBirdCall.unregisterVoIPPush(token:completionHandler:) or SendBirdCall.unregisterRemotePush(token:completionHandler:) methods.

func signOut() {
    SendBirdCall.deauthenticate { (error) in
        guard error == nil else {
            // Handle error.
        }

        // Handle the user's deauthentication.
    }
}