logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Offline login


Function introduction

Offline login is typically used when the application is cleared from memory, and the user relaunches the application by clicking on the application icon. In this scenario, the user can directly access the local SDK data without network connectivity or a successful login.

Note

Users can only perform an offline login using the UserID from their previous successful login; otherwise, the login will not succeed.

Technical principle

When the offline login interface is called, the ZIM SDK performs verification (UserID verification; if token-based login is used, it also verifies the token's validity period). After successful verification, the SDK returns a login success callback, allowing users to query local data, while the SDK internally attempts to establish a connection with the backend service automatically.

Implementation Steps

1. Offline Login

In the case of no network and the app being cleared, you can call the login interface with the isOfflineLogin parameter set to true in ZIMLoginConfig to achieve offline login using the user information from the last login. After successful login, you can access the local SDK data.

Note

We recommend caching the user information used for each login. When the app is opened, read the cache and implement the following logic:

  • If it is determined that the user did not use the UserID from the last online login, the isOfflineLogin should be set to false, and offline login will fail, not allowing the user to access the local SDK data for that UserID.
  • If the user used the UserID from the last online login, in order to achieve automatic login, speed up entering the app's main page, and pre-render the UI, the isOfflineLogin should be set to true to achieve offline login.

Definition of the Login Interface

Untitled
// This Login interface is the new login interface. The original login interface has been deprecated since version 2.13.0 and is no longer maintained.
login(userID: string, config: ZIMLoginConfig): Promise<void>;
1
Copied!
Parameter/CallbackTypeRequiredDescription
userIDstringYesThe user ID for the previous online login. It can contain up to 32 bytes in length, including digits, letters, and the following characters: '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '< ', '= ', '. ', '> ', '?', '@', '[', ']', '^', '_', '{','}' '|', '~'.
configZIMLoginConfigYesLogin configuration.
PromisevoidNoLogin operation callback.

The config parameter needs to be configured with the following parameters of the ZIMLoginConfig class:

ParameterTypeRequiredDescription
userNamestringNoUser name. A string with a maximum of 256 bytes. If empty, it will be the UserName set during the last login.
tokenstringYesToken authentication.
isOfflineLoginbooleanNoWhether this login is an offline login. If empty, it will be an online login.

Example

Untitled
var userID = '';
var config = {
    token: '', // The validity period cannot exceed 24 days. Request the developer server to obtain it.
               // When using AppSign authentication, this parameter is not required.
    userName: '',
    isOfflineLogin: true
};

zim.login(userID, config)
    .then(function() {
        // Offline login successful, can query SDK local data
    })
    .catch(function(err) {
        // Login failed
    });
1
Copied!

2 Accessing Local SDK Data

After a successful offline login, users can perform the following operations when offline (as of version 2.12):

CategoryInterface
User-relatedqueryUsersInfo
Group-related
Message Related
Call Invitation RelatedqueryCallInvitationList
Conversation Related

3. Listening for Events

Users can listen for the connectionStateChanged event notification to get the connection status between the SDK and the ZIM backend service.

When offline login is successful, connectionStateChanged returns ZIMConnectionState as Connecting (enum value 1).

When the user's network is restored and the ZIM SDK successfully reconnects to the ZIM backend service, connectionStateChanged returns ZIMConnectionState as Connected (enum value 2). Only when this happens, the ZIM SDK allows calling APIs that depend on a stable network connection and synchronizes backend data.

Previous

Multi-device login

Next

Blacklist management