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.
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.
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
false
. In this case, offline login will fail and the user will not be allowed to access the local SDK data for that UserID. - If the user uses 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
true
to achieve offline login.
Definition of the Login Interface
// 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.
Future<void> login(String userID,ZIMLoginConfig config);
Parameter | Type | Required | Description |
---|---|---|---|
userID | String | Yes | The user ID for the previous online login. It can contain up to 32 bytes in length, including digits, letters, and the following characters: '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '< ', '= ', '. ', '> ', '?', '@', '[', ']', '^', '_', '{','}' '|', '~'. |
config | ZIMLoginConfig | Yes | Login configuration. |
The parameter config
needs to be configured using the following parameters of the ZIMLoginConfig class:
Parameter | Type | Required | Description |
---|---|---|---|
userName | String | No | User name. A string with a maximum of 256 bytes. If empty, it will be the UserName set during the last login. |
token | String | No | Token.
|
isOfflineLogin | bool | No | Whether this login is an offline login. If empty, it will be an online login. |
Example
ZIMLoginConfig loginConfig = ZIMLoginConfig();
loginConfig.userName = 'username';
// This parameter is not required when using AppSign authentication
loginConfig.token = 'token';
// Whether it is an offline login
login.isOfflineLogin = true;
try {
await ZIM.getInstance()!.login('zego', loginConfig);
// Login successful
} on PlatformException catch (onError) {
// Login failed
}
2 Accessing Local SDK Data
After a successful offline login, users can perform the following operations when offline (as of version 2.12):
Category | Interface |
---|---|
User-related | queryUsersInfo |
Group-related | |
Message-related |
|
Call Invitation-related | queryCallInvitationList |
Conversation-related |
|
3 Event Listening
Users can listen to the onConnectionStateChanged event notification to get the connection status between the SDK and the ZIM backend service.
When offline login is successful, onConnectionStateChanged returns ZIMConnectionState as Connecting (enum value 1).
When the user's network is restored and the ZIM SDK successfully connects to the ZIM backend service, onConnectionStateChanged returns ZIMConnectionState as Connected (enum value 2). Only then does the ZIM SDK allow calling interfaces that depend on a stable network connection and synchronizing backend data.