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

Procedure

1. Log in to the app in offline status

In scenarios where there is no network connectivity and the application has been cleared from memory, you can make use of login interface. Set the isOfflineLogin in ZIMLoginConfig to trueto achieve offline login using the user information from the previous session. Once the login is confirmed as successful through the ZIMLoggedInCallback , you can then access the local SDK data.

Note

We recommend that you cache user information for each login. If the app is opened, the following logic is implemented when reading the cache:

  • If the user does not log in with the UserID for the previous online login, isOfflineLogin should be false. In this case, offline login is not supported, and local data of the SDK is inaccessible.
  • If the user logs in with the UserID for the previous online login, isOfflineLogin should be true to implement automatic login, accelerated access to the app homepage, and UI rendering in advance. In this case, offline login is supported.

Login method definition

// This login method is new, and the original method is no longer maintained since ZIM SDK 2.13.0.
virtual void login(const std::string &userID, const ZIMLoginConfig &config,
                    ZIMLoggedInCallback callback) = 0;
ParameterTypeRequiredDescription
userIDconst std::string &YesThe user ID for the previous online login. It can contain up to 32 bytes in length, including digits, letters, and the following characters: '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '< ', '= ', '. ', '> ', '?', '@', '[', ']', '^', '_', '{','}' '|', '~'.
configconst ZIMLoginConfig &YesLogin configuration.
callbackZIMLoggedInCallbackYesThe login operation callback.

The following table describes the parameters in the ZIMLoginConfig class that are used to configure the config parameter.

ParameterTypeRequiredDescription
userNamestd::stringNoThe username, which can contain up to 256 bytes in length. If the value is empty, the UserName for the previous login is used.
tokenstd::stringNoThe token.
  • This parameter is required for token-based authentication.
  • Do not specify it for AppSign-based authentication.
isOfflineLoginboolNoSpecifies whether this login is offline login. If the value is empty, the login is online login.

Sample call

zim::ZIMLoginConfig config;
config.userName = user_name;
// Do not specify this parameter for AppSign-based authentication.
config.token = token;
config.isOfflineLogin = true;

zim_sdk->login("user_id", config, [=](const zim::ZIMError &errorInfo) {
    if (errorInfo.code == zim::ZIMErrorCode::ZIM_ERROR_CODE_SUCCESS) {
        // Offline login is successful, and local data of the SDK can be queried.
    }
}];

2. Access local data of the SDK

After successful offline login, the following methods of the ZIM SDK 2.15.0 or earlier can be called when the network is disconnected.

CategoryMethod
User
Group
Message
Call invitationqueryCallInvitationList
Conversation
Cache management

3. Listen for the login event

Listen for the onConnectionStateChanged event to obtain the status of connection between the ZIM SDK and the ZIM backend.

After a successful offline login, the value of ZIMConnectionState in onConnectionStateChanged is Connecting (enumeration value: 1).

You can call network methods to synchronize backend data only when the ZIM SDK automatically connects to the ZIM backend upon network reconnection and the value of ZIMConnectionState in onConnectionStateChanged is Connected (enumeration value: 2).

Previous

Multi-device login

Next

Blacklist management

On this page

Back to top