login
API (Android / iOS and macOS / Windows) to log in.login
API are as follows:Parameter | Definition | Description |
UserID | Unique user ID | It can contain up to 32 bytes of letters (a-z and A-Z), digits (0-9), underscores (_), and hyphens (-). |
UserSig | Login ticket | It is calculated by your business server to ensure security. For more information, see Generating UserSig. |
login
API in the following scenarios:login
API returns the ERR_USER_SIG_EXPIRED (6206)
or ERR_SVR_ACCOUNT_USERSIG_EXPIRED (70001)
error code. In this case, you need to generate a new userSig
to log in again.onUserSigExpired
callback (Android / iOS and macOS / Windows) when users are online. In this case, you need to generate a new userSig
to log in again.onKickedOffline
callback (Android / iOS and macOS / Windows). In this case, you can prompt the user and call login
to log in again.login
API in the following scenarios:login
function, as the SDK will automatically go online.String userID = "your user id";String userSig = "userSig from your server";V2TIMManager.getInstance().login(userID, userSig, new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("sdk", "success");}@Overridepublic void onError(int code, String desc) {// The following error codes indicate an expired `userSig`, and you need to generate a new one for login again.// 1. ERR_USER_SIG_EXPIRED (6206)// 2. ERR_SVR_ACCOUNT_USERSIG_EXPIRED (70001)// Note: Do not call the login API in case of other error codes; otherwise, the SDK may enter an infinite loop of login.Log.i("sdk", "failure, code:" + code + ", desc:" + desc);}})
NSString *userID = @"your user id";NSString *userSig = @"userSig from your server";[[V2TIMManager sharedInstance] login:userID userSig:userSig succ:^{NSLog(@"success");} fail:^(int code, NSString *desc) {// The following error codes indicate an expired `userSig`, and you need to generate a new one for login again.// 1. ERR_USER_SIG_EXPIRED (6206)// 2. ERR_SVR_ACCOUNT_USERSIG_EXPIRED (70001)// Note: Do not call the login API in case of other error codes; otherwise, the SDK may enter an infinite loop of login.NSLog(@"failure, code:%d, desc:%@", code, desc);}]
class LoginCallback final : public V2TIMCallback {public:LoginCallback() = default;~LoginCallback() override = default;using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString userID = "your user id";V2TIMString userSig = "userSig from your server";auto callback = new LoginCallback;callback->SetCallback([=]() {std::cout << "success";delete callback;},[=](int error_code, const V2TIMString& error_message) {// The following error codes indicate an expired `userSig`, and you need to generate a new one for login again.// 1. ERR_USER_SIG_EXPIRED (6206)// 2. ERR_SVR_ACCOUNT_USERSIG_EXPIRED (70001)// Note: Do not call the login API in case of other error codes; otherwise, the SDK may enter an infinite loop of login.std::cout << "failure, code:" << error_code << ", desc:" << error_message.CString();delete callback;});V2TIMManager::GetInstance()->Login(userID, userSig, callback);
getLoginUser
(Android / iOS and macOS / Windows) to get the UserID
.
If the login fails, the UserID
will be empty.// Get the `UserID` of the logged-in userString loginUserID = V2TIMManager.getInstance().getLoginUser();
// Get the `UserID` of the logged-in userNSString *loginUserID = [[V2TIMManager sharedInstance] getLoginUser];
// Get the `UserID` of the logged-in userV2TIMString loginUserID = V2TIMManager::GetInstance()->GetLoginUser();
getLoginStatus
(Android / iOS and macOS / Windows) to get the login status. If a user is logged in or logging in, don't call the login API frequently. The SDK supports the following login statuses:Login Status | Description |
V2TIM_STATUS_LOGINED | Logged in |
V2TIM_STATUS_LOGINING | Logging in |
V2TIM_STATUS_LOGOUT | Not logged in |
int loginStatus = V2TIMManager.getInstance().getLoginStatus();
V2TIMLoginStatus loginStatus = [[V2TIMManager sharedInstance] getLoginStatus];
V2TIMLoginStatus loginStatus = V2TIMManager::GetInstance()->GetLoginStatus();
login
API to log in, if the limit specified by the multi-client login policy for your account is exceeded, a newly logged in instance will kick out an earlier one. If you have called addIMSDKListener
(Android / iOS and macOS / Windows) during the initialization to add the SDK listener, an earlier login instance will receive the onKickedOffline
callback (Android / iOS and macOS / Windows).logout
API (Android / iOS and macOS/ Windows) to log out of the SDK, after which you will no longer receive new messages. Note that in this case, you also need to call unInitSDK
(Android / iOS and macOS / Windows) after the logout to uninitialize the SDK.V2TIMManager.getInstance().logout(new V2TIMCallback() {@Overridepublic void onSuccess() {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
[[V2TIMManager sharedInstance] logout:^{NSLog(@"success");} fail:^(int code, NSString *desc) {NSLog(@"failure, code:%d, desc:%@", code, desc);}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString userID = "your user id";V2TIMString userSig = "userSig from your server";auto callback = new Callback{};callback->SetCallback([=]() {std::cout << "success";delete callback;},[=](int error_code, const V2TIMString& error_message) {std::cout << "failure, code:" << error_code << ", desc:" << error_message.CString();delete callback;});V2TIMManager::GetInstance()->Logout(callback);
login
to switch between accounts in the application.alice
to bob
, just log bob in. You don't need to explicitly call logout alice
, as this operation will be handled automatically inside the SDK.
Was this page helpful?