This article describes how to implement user login, determine user login status, and handle login status exceptions using the ZIM SDK.
Implement user login
First-time user login
Set isOfflineLogin to false in ZIMLoginConfig.
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
String userID = "";
ZIMLoginConfig config = new ZIMLoginConfig();
config.userName = ""; // If you do not need to modify the userName during login, leave this field empty.
config.isOfflineLogin = false;
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication].
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
config.token = "";
zim.login(userID, config, new ZIMLoggedInCallback() {
@Override
public void onLoggedIn(ZIMError error) {
// The developer can determine whether the login is successful based on ZIMError.
}
});
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
String userID = "";
ZIMLoginConfig config = new ZIMLoginConfig();
config.userName = ""; // If you do not need to modify the userName during login, leave this field empty.
config.isOfflineLogin = false;
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication].
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
config.token = "";
zim.login(userID, config, new ZIMLoggedInCallback() {
@Override
public void onLoggedIn(ZIMError error) {
// The developer can determine whether the login is successful based on ZIMError.
}
});
ZIMLoginConfig *config = [[ZIMLoginConfig alloc]init];
config.userName = @""; // If you do not need to modify the userName during login, leave this field empty.
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication].
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
config.token = @"";
config.isOfflineLogin = true;
[zim loginWithUserID:userID config:config callback:^(ZIMError * _Nonnull errorInfo) {
// Fill in the login callback here.
}];
ZIMLoginConfig *config = [[ZIMLoginConfig alloc]init];
config.userName = @""; // If you do not need to modify the userName during login, leave this field empty.
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication].
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
config.token = @"";
config.isOfflineLogin = true;
[zim loginWithUserID:userID config:config callback:^(ZIMError * _Nonnull errorInfo) {
// Fill in the login callback here.
}];
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
zim::ZIMLoginConfig config;
config.userName = ""; // The user's nickname. Leave empty if you do not want to modify it.
// For Token authentication, pass in your Token. For details, refer to [Use Token authentication] or use a temporary Token.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token field as an empty string.
config.token = "";
config.isOfflineLogin = false;
zim_->login(userID, config, [=](zim::ZIMError errorInfo){
// You can obtain the login result here and execute user code based on the error code.
});
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
zim::ZIMLoginConfig config;
config.userName = ""; // The user's nickname. Leave empty if you do not want to modify it.
// For Token authentication, pass in your Token. For details, refer to [Use Token authentication] or use a temporary Token.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token field as an empty string.
config.token = "";
config.isOfflineLogin = false;
zim_->login(userID, config, [=](zim::ZIMError errorInfo){
// You can obtain the login result here and execute user code based on the error code.
});
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication]. Web platform applications only support Token authentication.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
try{
ZIMLoginConfig loginConfig = ZIMLoginConfig();
// The user's nickname. Leave empty if you do not want to modify it.
loginConfig.userName = 'userName';
// If using token as the login authentication method, fill in this parameter. Otherwise, leave it empty.
loginConfig.token = '';
// Whether this login is an offline login. For details, refer to the offline login documentation.
loginConfig.isOfflineLogin = false;
await ZIM.getInstance()?.login('zego', loginConfig);
// Login succeeded. Write the business logic for successful login.
} on PlatformException catch(onError){
// Login failed.
// For the error code of the login failure, refer to the error code documentation.
onError.code;
// The error message of the login failure.
onError.message;
}
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication]. Web platform applications only support Token authentication.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
try{
ZIMLoginConfig loginConfig = ZIMLoginConfig();
// The user's nickname. Leave empty if you do not want to modify it.
loginConfig.userName = 'userName';
// If using token as the login authentication method, fill in this parameter. Otherwise, leave it empty.
loginConfig.token = '';
// Whether this login is an offline login. For details, refer to the offline login documentation.
loginConfig.isOfflineLogin = false;
await ZIM.getInstance()?.login('zego', loginConfig);
// Login succeeded. Write the business logic for successful login.
} on PlatformException catch(onError){
// Login failed.
// For the error code of the login failure, refer to the error code documentation.
onError.code;
// The error message of the login failure.
onError.message;
}
// During login, the developer needs to generate a token as described in the "Use Token authentication" document.
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
const userID = 'xxxx';
const config: ZIMLoginConfig = {
userName: 'xxxx',
token: '',
customStatus: '',
isOfflineLogin: false,
};
zim.login(userID, config)
.then(() => {
// Login succeeded.
})
.catch((err: ZIMError) => {
// Login failed.
});
// During login, the developer needs to generate a token as described in the "Use Token authentication" document.
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
const userID = 'xxxx';
const config: ZIMLoginConfig = {
userName: 'xxxx',
token: '',
customStatus: '',
isOfflineLogin: false,
};
zim.login(userID, config)
.then(() => {
// Login succeeded.
})
.catch((err: ZIMError) => {
// Login failed.
});
Auto-login on app startup
If the user has previously logged in and has not actively logged out, you can choose one of the following methods based on your business requirements when the app starts:
Auto-login as the previous user
Create a ZIM instance on the launch page and call login with isOffline set to true.
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
String userID = ""; // The userID of the previous login.
ZIMLoginConfig config = new ZIMLoginConfig();
config.userName = "";
config.isOffline = true;
config.token = ""; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
zim.login(userID, config, new ZIMLoggedInCallback() {
@Override
public void onLoggedIn(ZIMError error) {
// The developer can determine whether the login is successful based on ZIMError.
}
});
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
String userID = ""; // The userID of the previous login.
ZIMLoginConfig config = new ZIMLoginConfig();
config.userName = "";
config.isOffline = true;
config.token = ""; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
zim.login(userID, config, new ZIMLoggedInCallback() {
@Override
public void onLoggedIn(ZIMError error) {
// The developer can determine whether the login is successful based on ZIMError.
}
});
ZIMLoginConfig *config = [[ZIMLoginConfig alloc]init];
config.userName = @""; // If you do not need to modify the userName during login, leave it empty.
config.token = @""; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
config.isOffline = true;
[zim loginWithUserID:userID config:config callback:^(ZIMError * _Nonnull errorInfo) {
// Fill in the login callback here.
}];
ZIMLoginConfig *config = [[ZIMLoginConfig alloc]init];
config.userName = @""; // If you do not need to modify the userName during login, leave it empty.
config.token = @""; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
config.isOffline = true;
[zim loginWithUserID:userID config:config callback:^(ZIMError * _Nonnull errorInfo) {
// Fill in the login callback here.
}];
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
zim::ZIMLoginConfig config;
config.userName = ""; // The user's nickname. Leave empty if you do not want to modify it.
config.token = ""; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
config.isOffline = true;
zim_->login(userID, config, [=](zim::ZIMError errorInfo){
// You can obtain the login result here and execute user code based on the error code.
});
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
zim::ZIMLoginConfig config;
config.userName = ""; // The user's nickname. Leave empty if you do not want to modify it.
config.token = ""; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
config.isOffline = true;
zim_->login(userID, config, [=](zim::ZIMError errorInfo){
// You can obtain the login result here and execute user code based on the error code.
});
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication]. Web platform applications only support Token authentication.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
try{
ZIMLoginConfig loginConfig = ZIMLoginConfig();
// The user's nickname. Leave empty if you do not want to modify it.
loginConfig.userName = 'userName';
loginConfig.token = ''; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
// Whether this login is an offline login. For details, refer to the offline login documentation.
loginConfig.isOfflineLogin = true;
await ZIM.getInstance()?.login('zego', loginConfig);
// Login succeeded. Write the business logic for successful login.
} on PlatformException catch(onError){
// Login failed.
// For the error code of the login failure, refer to the error code documentation.
onError.code;
// The error message of the login failure.
onError.message;
}
// For Token authentication, the developer needs to fill in the Token generated by the "Use Token authentication" document. For details, refer to [Use Token authentication]. Web platform applications only support Token authentication.
// For AppSign authentication (the default authentication method since version 2.3.0 or later), leave the Token parameter as an empty string.
try{
ZIMLoginConfig loginConfig = ZIMLoginConfig();
// The user's nickname. Leave empty if you do not want to modify it.
loginConfig.userName = 'userName';
loginConfig.token = ''; // If you use token authentication, you still need to pass in the token so that the SDK can restore the online state when the network is available.
// Whether this login is an offline login. For details, refer to the offline login documentation.
loginConfig.isOfflineLogin = true;
await ZIM.getInstance()?.login('zego', loginConfig);
// Login succeeded. Write the business logic for successful login.
} on PlatformException catch(onError){
// Login failed.
// For the error code of the login failure, refer to the error code documentation.
onError.code;
// The error message of the login failure.
onError.message;
}
// During login, the developer needs to generate a token as described in the "Use Token authentication" document.
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
const userID = 'xxxx';
const config: ZIMLoginConfig = {
userName: 'xxxx',
token: '',
customStatus: '',
isOfflineLogin: true,
};
zim.login(userID, config)
.then(() => {
// Login succeeded.
})
.catch((err: ZIMError) => {
// Login failed.
});
// During login, the developer needs to generate a token as described in the "Use Token authentication" document.
// userID: a string of up to 32 bytes. Supports only digits, English letters, and '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'.
// userName: a string of up to 256 bytes. No special character restrictions.
const userID = 'xxxx';
const config: ZIMLoginConfig = {
userName: 'xxxx',
token: '',
customStatus: '',
isOfflineLogin: true,
};
zim.login(userID, config)
.then(() => {
// Login succeeded.
})
.catch((err: ZIMError) => {
// Login failed.
});
When receiving an offline push of iOS VoIP or Android CallStyle type:
Create a ZIM instance and call login with isOffline set to false.
After the state changes to CONNECTED in onConnectionStateChanged (the user's login status has changed to Online), call callAccept, callReject, callEnd, and other APIs.
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onConnectionStateChanged(ZIM zim,
ZIMConnectionState state,
ZIMConnectionEvent event,
JSONObject extendedData) {
if (state == ZIMConnectionState.CONNECTED) {
if (userEverClickedCallAccept) {
zim.callAccept();
}
}
}
});
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onConnectionStateChanged(ZIM zim,
ZIMConnectionState state,
ZIMConnectionEvent event,
JSONObject extendedData) {
if (state == ZIMConnectionState.CONNECTED) {
if (userEverClickedCallAccept) {
zim.callAccept();
}
}
}
});
Determine user login status
ZIM does not provide a callback for user login status changes in ZIMEventHandler. You can determine the current user's login status through the onConnectionStateChanged connection status change callback.
When the user's status changes, use the following conditions to determine the current user login status:
User status
Condition
Online
onConnectionStateChanged is triggered, and the connection state is CONNECTED.
Offline
onConnectionStateChanged is triggered, and the connection state is RECONNECTING.
Not Logged In
onConnectionStateChanged has never been triggered (i.e., login has never been called), or the connection state is DISCONNECTED.
When the user's device experiences a weak network or network disconnection, the SDK automatically triggers reconnection, and onConnectionStateChanged returns the state of RECONNECTING and the event of LOGIN_INTERRUPTED.
Starting from version 2.9.0, the SDK automatically reconnects at an appropriate frequency. Developers do not need to call the login API for reconnection at the business layer.
Reconnection results:
Reconnection succeeded: The state of CONNECTED and the event of SUCCESS are returned.
Reconnection failed: The state of DISCONNECTED and the corresponding event are returned. Calling other APIs will report an error with the error code 6000121 (not logged in).
Developers should display a UI prompt to the user when a network disconnection event is detected. If the SDK cannot reconnect, implement fallback logic (e.g., redirecting to the login page). Do not attempt reconnection in the application layer code.
Account kicked out
When multi-device login is not enabled and another device logs in with the same account (userID) already logged in on this device, this device will be kicked offline, and the SDK will not automatically reconnect.
At this time, onConnectionStateChanged returns the state of DISCONNECTED and the event of KICKED_OUT. Calling other APIs will report an error with the error code 6000121 (not logged in).
Display a prompt to the user and implement fallback logic (e.g., redirecting to the login page). Do not attempt reconnection in the application layer code.
Token expired
When the AppID is configured with Token authentication and the Token is about to expire, the onTokenWillExpire callback is triggered.
After obtaining a new Token, pass it to the SDK through the renewToken API.
If no new Token is passed in, the SDK will disconnect from the server after the Token expires. onConnectionStateChanged returns the state of DISCONNECTED and the event of TokenExpired. Calling other APIs will report an error with the error code 6000121 (not logged in).
Display a prompt to the user and implement fallback logic (e.g., redirecting to the login page). Do not attempt reconnection in the application layer code.