iOS
UIKit Authentication

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.

Objective-C
Swift
Light Color Skin
Copy
[SBUGlobals setAccessToken:{ACCESS_TOKEN}];
Light Color Skin
Copy
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 does not 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 information which was set in the SBUGlobal.

In case you want to connect to Sendbird server at a particular timing, you can use the code as below:

Objective-C
Swift
Light Color Skin
Copy
[SBUMain connectWithCompletionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    ...        
}];
Light Color Skin
Copy
SBUMain.connect { (user, error) in
    ...            
}

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

Objective-C
Swift
Light Color Skin
Copy
[SBUMain disconnectWithCompletionHandler:^{}];
Light Color Skin
Copy
SBUMain.disconnect {}

Register for push notifications

Push notifications are a type of notification sent to your users’ devices when an application is in the background. Push notifications for iOS applications 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 application on iOS devices. In order to use this feature, you need to register the device tokens of your client app users to Sendbird server through the AppDelegate.

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

Objective-C
Swift
Light Color Skin
Copy
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    ...

    [SBUMain registerPushWithDeviceToken:deviceToken completionHandler:^(BOOL success) {
    }];
}
Light Color Skin
Copy
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    ...

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

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


Unregister for push notifications

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

Objective-C
Swift
Light Color Skin
Copy
// If you want to unregister the current device only, call this method.
[SBUMain unregisterPushTokenWithcompletionHandler:^(BOOL success) {

}];

// If you want to unregister all devices of the user, call this method.
[SBUMain unregisterAllPushTokenWithcompletionHandler:^(BOOL success) {

}];
Light Color Skin
Copy
// 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 SBUGlobal.CurrentUser is set and connected to Sendbird server.


Update user profile

When a CurrentUser is created with a unique ID of the user, UIKit automatically generates the 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 SBUGlobal.CurrentUser is set and connected to Sendbird server.

Objective-C
Swift
Light Color Skin
Copy
[SBUMain updateUserInfoWithNickname:{NICKNAME} profileUrl:{PROFILE_URL} completionHandler:^(SBDError * _Nullable) { 
    ...
}];
Light Color Skin
Copy
SBUMain.updateUserInfo(nickname: {NICKNAME}, profileUrl: {PROFILE_URL}) { (error) in
    ...            
}