Chat UIKit iOS UIKit v3
Chat UIKit iOS UIKit
Chat UIKit
iOS UIKit
Version 3

Authentication

Copy link

In order to use the features of Sendbird UIKit for iOS in your client apps, a SendBirdUI 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

Copy link

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.

SBUGlobals.accessToken = {ACCESS_TOKEN}

Connect to Sendbird server

Copy link

The SendbirdUI.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 SendbirdUI.connect() method also automatically updates the user profile on Sendbird server.

When the view controller is called in UIKit, the connection is automatically made with the currentUser which should be already 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:

SendbirdUI.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

Copy link

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).

SendbirdUI.disconnect {}

Register for push notifications

Copy link

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 user's device token to Sendbird server through the AppDelegate.

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

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    SendbirdUI.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

Copy link

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

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

}

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

}

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


Update user profile

Copy link

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.

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