iOS
Authentication

Authentication

In order to use the abilities of the iOS SDK in your client apps, a SendBird instance must be initiated in each client app through user authentication with SendBird server. The instance communicates and interacts with the server based on authenticated user account, and allows the user to the abilities. The following explains how to authenticate your user with the server.


Initialize with APP_ID

To use chat features, you must first initialize a SendBird instance by passing the APP_ID assigned to your SendBird application to the initWithApplicationId: method as a parameter. Generally, initialization is implemented in the user login view controller.

Objective-C
Swift
Light Color Skin
Copy
[SBDMain initWithApplicationId:APP_ID];
Light Color Skin
Copy
SBDMain.initWithApplicationId(APP_ID)

Connect to SendBird server with a user ID

By default, Sendbird server can authenticate a user just by unique user ID. Then the server queries the database to check for a match upon the request for connection. If there is no matching user ID found, the server creates a new user account with the user ID. The ID must be unique within a SendBird application to be distinguished from each other, such as a hashed email address or phone number in your service.

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

Note: In the Event Delegate page, you can find detailed information on the usages of iOS SDK's delegates and callbacks.

Objective-C
Swift
Light Color Skin
Copy
[SBDMain connectWithUserId:USER_ID completionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    if (error != nil) { // Error.
        return;
    }
}];
Light Color Skin
Copy
SBDMain.connect(withUserId: USER_ID, completionHandler: { (user, error) in 
    guard error == nil else {   // Error.
        return
    }
})

Note: You must connect to SendBird server before calling any methods through the SDK (apart from initializing a SendBird instance). If you attempt to call a method without connecting, a SBDErrorConnectionRequired (800101) error would be returned.


Connect to SendBird server with a user ID and an access token

Using the Platform API, you can create a user along with their own access token, or issue an access token for an existing user. Once an access token is issued, a user is required to provide the access token when logging in to the SendBird application.

  1. Using the user creation API, you can create a SendBird user account with your user's information when the user signs up or in to your service.
  2. Save the user ID along with the issued access token to your persistent storage which is securely managed.
  3. When the user attempts to log in to the application, load the user ID and access token from the storage, and then pass them to the connectWithUserId:accessToken:completionHandler: method.
  4. For security, we recommend updating the user's access token periodically by issuing a new token to replace.

Note: You are able to prevent users without an access token from logging in to the application or restrict their access to read and write messages in your dashboard: Settings > Application > Security > Access Token Policy.

Objective-C
Swift
Light Color Skin
Copy
[SBDMain connectWithUserId:USER_ID accessToken:ACCESS_TOKEN completionHandler:^(SBDUser * _Nullable user, SBDError * _Nullable error) {
    if (error != nil) { // Error.
        return;
    }
}];
Light Color Skin
Copy
SBDMain.connect(withUserId: USER_ID, accessToken: ACCESS_TOKEN, completionHandler: { (user, error) in
    guard error == nil else {   // Error.
        return
    }
})

Note: For security reasons, you can also use a session token when a user logs in to SendBird server instead of an access token. For more information, see the Access token vs. Session token section from the Platform API guide.


Disconnect from SendBird server

A user should be disconnected from SendBird server when the user no longer needs to receive messages from an online state. But the user can receive push notifications for new messages from their joined group channels.

When the user is disconnected from the server, all event delegates in the user's client app, which were registered by the addChannelDelegate:identifier: or addConnectionDelegate:identifier: method, stop receiving event callbacks from the server. The disconnecting also flushes all internally cached data in the client app, such as the channels that are cached when the getChannelWithUrl:completionHandler: method of SBDOpenChannel or SBDGroupChannel is called.

Note: By default, most of the data related to users, channels, and messages are internally cached in the SendBird instance of a user's client app, which are retrieved by the corresponding query instances or received through the event delegates.

Objective-C
Swift
Light Color Skin
Copy
[SBDMain disconnectWithCompletionHandler:^{
    // A current user is disconnected from SendBird server.
}];
Light Color Skin
Copy
SBDMain.disconnect(completionHandler: {
    // A current user is disconnected from SendBird server.
})

Update user profile

Using the updateCurrentUserInfoWithNickName: method, you can update a user's nickname and profile image when needed, as well as their profile picture with a URL.

Objective-C
Swift
Light Color Skin
Copy
[SBDMain updateCurrentUserInfoWithNickname:NICKNAME profileUrl:PROFILE_URL completionHandler:^(SBDError * _Nullable error) {
    if (error != nil) { // Error.
        return;
    }
}];
Light Color Skin
Copy
SBDMain.updateCurrentUserInfo(withNickname: NICKNAME, profileUrl: PROFILE_URL, completionHandler: { (error) in
    guard error == nil else {   // Error.
        return
    }
})

Or, you can pass in an image file directly.

Objective-C
Swift
Light Color Skin
Copy
[SBDMain updateCurrentUserInfoWithNickname:NICKNAME profileImage:PROFILE_FILE completionHandler:^(SBDError * _Nullable error) {
    if (error != nil) { // Error.
        return;
    }
}];
Light Color Skin
Copy
SBDMain.updateCurrentUserInfo(withNickname: NICKNAME, profileImage: PROFILE_FILE, completionHandler: { (error) in
    guard error == nil else {   // Error.
        return
    }
})