Authentication with the Service Chat SDK for iOS

The Service Chat SDK provides an authentication mechanism that allows your users to access user-specific information in Service Cloud. To authenticate, create an SCSAuthenticationSettings object and pass it to the SDK.

The legacy chat product is scheduled for retirement on February 14, 2026, and is in maintenance mode until then. During this phase, you can continue to use chat, but we no longer recommend that you implement new chat channels. To avoid service interruptions to your customers, migrate to Messaging for In-App and Web before that date. Messaging offers many of the chat features that you love plus asynchronous conversations that can be picked back up at any time. Learn about chat retirement in Help.

Authentication with the Service Chat SDK uses an SCSAuthenticationSettings object. Create this object with a client ID and a dictionary containing authentication settings. This authentication settings dictionary must contain a URL for your org (SCSOAuth2JSONKeyInstanceUrl) and an access token (SCSOAuth2JSONKeyAccessToken). If your OAuth2 flow supports refresh tokens, include a refresh token (SCSOAuth2JSONKeyRefreshToken) to the authentication settings.

In Swift:

In Objective-C:

If you’re using the Salesforce Mobile SDK, we provide a helper method that allows you to construct an SCSAuthenticationSettings object directly from the Mobile SDK user account. You can use this sample code after you’ve successfully logged in a user.

In Swift:

In Objective-C:

For developers who plan to use the Salesforce Mobile SDK for authentication, the Mobile SDK Developer’s Guide contains authentication instructions. If you’re using a Salesforce Experience Cloud site, be sure to configure the login endpoint as described in the Salesforce Mobile SDK documentation (Configure the Login Endpoint). This documentation describes how to use the SFDCOAuthLoginHost property in your info.plist file to create a custom login URI.

If you plan to use remote push notification to alert the user when an event occurs in your org, call registerForPushNotifications on the SCSAuthenticationSettings object. To learn more, see Notifications with the Service Chat SDK for iOS.

However you create an SCSAuthenticationSettings object, pass it to the Service Chat SDK during the authentication flow.

You can either authenticate on-demand when the SDK calls serviceCloud(shouldAuthenticateServiceType:completion:) in your SCServiceCloudDelegate implementation, or you can authenticate explicitly (that is, before the app attempts to show the relevant UI) using the setAuthenticationSettings(settings:forServiceType:completion:) method in the ServiceCloud shared instance.

With on-demand authentication, you perform the authentication asynchronously, after the SDK calls your serviceCloud(shouldAuthenticateServiceType:completion:) delegate method. Once authenticated, pass the SCSAuthenticationSettings object to the completion block that you’re given in the serviceCloud(shouldAuthenticateServiceType:completion:) method.

The following sequence diagram illustrates the basic authentication flow for on-demand authentication.

OAuth on demand diagram

Alternatively, you can explicitly authenticate before any UI appears that requires authentication. Call the setAuthenticationSettings(settings:forServiceType:completion:) method in the ServiceCloud shared instance using the SCSAuthenticationSettings object.

The following sequence diagram illustrates the authentication flow for explicit authentication.

OAuth explicit diagram

To programmatically log out a user, call setAuthenticationSettings(settings:forServiceType:completion:) using nil for the SCSAuthenticationSettings argument.

Implement serviceCloud(authenticationFailed:forServiceType:) in your SCServiceCloudDelegate object to handle error conditions. The SDK calls this method if the access token expires, and for any other scenario that results in an authentication failure. If you return true or YES, the SDK assumes that you want to proceed, and it subsequently calls serviceCloud(shouldAuthenticateServiceType:completion:) to give you a chance to send updated authentication information. If you return false or NO, the SDK goes back to the guest user state.

The error argument in this method contains information about the error. The code property on this object contains the error code. The following errors are the most common error codes you can encounter:

Error CodeDescription
SCServiceUserSessionExpiredOrInvalidError (401)Occurs when the session has expired or the ID is invalid. Use the refresh token to acquire another access token and then update the account property, as shown in the next step.
SCServiceUserRequestRefusedError (403)Occurs when the user does not have sufficient access for the request. Verify that the logged in user has sufficient credentials.
SCServiceUserResourceNotFoundError (404)Occurs when the requested resource was not found. Check the URI and other parameters for errors.

See Status Codes and Error Responses for a full set of possible error codes.

The following sample code illustrates how to implement an SCServiceCloudDelegate object to handle authentication.