Authentication

To use SendBird's features, first do the following authentication tasks.


Initialize with APP_ID

To use chat features, you must initialize SendBird using the APP_ID assigned to your SendBird application. Generally, initialization is implemented in the user login view controller.

[SBDMain initWithApplicationId:APP_ID];
SBDMain.initWithApplicationId(APP_ID)

Connect to SendBird server with UserID

By default, a SendBird server requires only a UserID to join a channel. When requesting connection, the SendBird server queries its user database for a matching UserID. If it finds that the UserID has not been registered yet, a new user account is created. The UserID can be any unique string id, such as an email address or a UID from your database.

This simple authentication procedure is useful when you are in development or if your service doesn't require additional security.

Note: Explanation on the usages of iOS SDK's delegates and callbacks can be found under the Event Delegate section.

[SBDMain connectWithUserId:USER_ID completionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    // ...
}];
SBDMain.connect(withUserId: USER_ID, completionHandler: { (user, error) in 
    // ...
})

Connect to SendBird server with UserID and Access Token

With the Platform API, you can create a user with an access token, or you can issue an access token for an existing user. Once an access token is issued, you are required to provide the user's token in the login method.

  1. Create a SendBird user account via the Platform API when your user signs up on your service.
  2. Save the access token to your secured persistent store.
  3. Load the access token in your client and pass it to the SendBird login method.
  4. For security reasons, we recommend that you periodically update your access token by issuing a new token to replace the previous one.

Note: You can set restrictions for users without access tokens in your Dashboard settings. These settings can be found under Security - Access Token Policy.

[SBDMain connectWithUserId:USER_ID accessToken:ACCESS_TOKEN completionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    // ...
}];
SBDMain.connect(withUserId: USER_ID, accessToken: ACCESS_TOKEN, completionHandler: { (user, error) in
    // ...
})

Disconnect from SendBird server

You must disconnect from a SendBird server when your user no longer needs to receive messages from an online state. But users receive group channel messages through Push Notifications.

The disconnecting removes all registered delegates and callbacks. That is, it removes all event delegates added through addChannelDelegate:identifier: or addConnectionDelegate:identifier: method of SBDMain. It also flushes all internally cached data, such as the channels that are cached when getChannelWithUrl:completionHandler: method of SBDOpenChannel or getChannelWithUrl:completionHandler: method of SBDGroupChannel is called.

[SBDMain disconnectWithCompletionHandler:^{
    // ...
}];
SBDMain.disconnect(completionHandler: {
    // ...
})

Update user profile

You can update a user's nickname and profile image. Call updateCurrentUserInfoWithNickName: method to update a user's nickname, as well as their profile picture with a URL.

[SBDMain connectWithUserId:USER_ID completionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    [SBDMain updateCurrentUserInfoWithNickname:NICKNAME profileUrl:PROFILE_URL completionHandler:^(SBDError * _Nullable error) {
        // ...
    }];
}];
SBDMain.connect(withUserId: USER_ID, completionHandler: { (user, error) in 
    SBDMain.updateCurrentUserInfo(withNickname: NICKNAME, profileUrl: PROFILE_URL, completionHandler: { (error) in
        // ...
    })
})

Or, you can pass in an image file directly.

[SBDMain connectWithUserId:USER_ID completionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    [SBDMain updateCurrentUserInfoWithNickname:NICKNAME profileImage:PROFILE_FILE completionHandler:^(SBDError * _Nullable error) {
        // ...
    }];
}];
SBDMain.connect(withUserId: USER_ID, completionHandler: { (user, error) in 
    SBDMain.updateCurrentUserInfo(withNickname: NICKNAME, profileImage: PROFILE_FILE, completionHandler: { (error) in
        // ...
    })
})