Home
/
UIKit
/
iOS

Authentication

In order to use the features of Sendbird UIKit for iOS in your client apps, a SendBirdUIKit instance must be initiated in each client app through user authentication with Sendbird server. The instance communicates and interacts with the server using an authenticated user account, and is allowed to use the UIKit's features. This page explains how to authenticate your user with the server.


Set an access token

Sendbird server authenticates requests from client apps through two methods: One is using only user ID, the other is using a user ID along with the user's access token.

The user ID authentication can be useful for applications in development or existing services where additional security is not necessarily needed. On the other hand, the access token authentication generates access tokens for each user through Chat Platform API. Once you request and obtain a token for a user, you need to use that specific access token to authenticate the user.

SwiftObjective-C
SBUGlobals.AccessToken = {ACCESS_TOKEN}

Connect to Sendbird server

The SBUMain.connect() method connects a user to Sendbird server using the preset information of the CurrentUser. If the user ID used during the connection attempt doesn't exist on Sendbird server, a new user account is created with the specified user ID. The SBUMain.connect() method also automatically updates the user profile on Sendbird server.

When the ViewController is called in UIKit, the connection is automatically made with the CurrentUser which was set in the SBUGlobals.

With local caching added to Sendbird Chat SDK, the latest user instance may be returned through the callback even when the user is offline. The local caching functionality stores message and channel data in the local storage and Sendbird server. As a result, even when a user is not connected to the server, the user information stored in the local cache is returned through the callback along with an error indicating the offline status.

Refer to the code below to see how to connect a user to Sendbird server:

SwiftObjective-C
SBUMain.connect { (user, error) in
    guard let user = user else {
        // The user is offline and you can't access any user information stored in the local cache.
        return
    }
    if let error = error {
        // The user is offline but you can access user information stored in the local cache.
    }
    else {
        // The user is online and connected to the server.
    }
}

Disconnect from Sendbird server

When a user doesn’t need to receive messages from Sendbird server, it is recommended to disconnect the user from the server. Even after disconnected, the user can still receive push notifications for new messages delivered by APNs (Apple Push Notification Service).

SwiftObjective-C
SBUMain.disconnect {}

Register for push notifications

Push notifications are a type of notification sent to your user's device when a client app is running in the background. Push notifications for the client app will contain a payload created by Sendbird and be delivered through APNs. Sendbird server will communicate with APNs whenever needed and APNs will send a push notification to the client app on iOS devices. In order to use this feature, you need to register the client app user's device token to Sendbird server through the AppDelegate.

Note : APNs should be set up in advance in order to send push notifications.

SwiftObjective-C
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    ...

    SBUMain.registerPush(deviceToken: deviceToken) { (success) in
    }
}

Note : You can use these methods when the SBUGlobals.CurrentUser is set and connected to Sendbird server.


Unregister for push notifications

You should unregister a user's device token from Sendbird server if you don’t want to send push notifications to the user.

SwiftObjective-C
// If you want to unregister the current device only, call this method.
SBUMain.unregisterPushToken { (success) in

}

// If you want to unregister all devices of the user, call this method.
SBUMain.unregisterAllPushToken { (success) in

}

Note : You can use these methods when the SBUGlobals.CurrentUser is set and connected to Sendbird server.


Update user profile

When a CurrentUser is created with a unique ID, UIKit automatically generates nickname and profileImage of the user based on the specified userId. You can also modify these properties by yourself when needed.

Note : You can use this method when the SBUGlobals.CurrentUser is set and connected to Sendbird server.

SwiftObjective-C
SBUMain.updateUserInfo(nickname: {NICKNAME}, profileUrl: {PROFILE_URL}) { (error) in
    guard error == nil else {
        // Handle error.
    }

    ...
}