logo
In-app Chat
SDK Error Codes
On this page

User status management


Note

To use this feature, please subscribe to the Enterprise Plan.

Feature overview

User status includes user online status and custom status:

  • User online status ( ZIMUserOnlineStatus ): This can be divided into three states - online, offline, and logged out, as defined by ZIM. These states cannot be modified by developers.
    • Online: When the login interface is actively called and a network connection is maintained, the user's status changes to online.
    • Logged out: After actively calling the logout interface, the user's status changes to logged-out state.
    • Offline: After calling the login interface, if the app is force-closed, the screen is locked, or it is moved to the background causing the network connection to break, the user's status changes to offline.
  • Custom status: You can define your own statuses as needed, such as Do Not Disturb, Busy, etc.

This article explains how to set a custom status, as well as how to subscribe to and query user statuses.

Set custom status

You can set a custom status using any of the following methods:

Note
  • The maximum length of customStatus is 64 bytes, and it is valid for 1 day.
  • The frequency limit for the updateUserCustomStatus interface is 1 time/second.
Set on Login
Update Custom Status
ZIMLoginConfig* config = [[ZIMLoginConfig alloc] init];
config.customStatus = @"Busy"; // Default is an empty string. If it's an empty string, the user's custom status will not be modified during login.
1
Copied!
[[ZIM getInstance] updateUserCustomStatus:@"Do Not Disturb" callback:^(NSString * _Nonnull customStatus, ZIMError * _Nonnull errorInfo) {

}];
1
Copied!

Subscribe to user status

To continuously monitor the status of certain users, you can call the subscribeUsersStatus interface. Pass the target user IDs in batches to the parameter userIDs (up to 100 registered users, excluding the subscriber themselves). In the config, pass the subscriptionDuration (the duration for which the subscription is valid, during which the user status changes of the target users will be continuously monitored), thereby adding the subscription of the target users' user status to the current user's subscription list.

Note
  • A single user can subscribe to a maximum of 3,000 people. When the number of subscribed users exceeds 3,000, the earliest subscribed users will be overwritten.
  • After subscribing to other users' user statuses, even if the user logs out and relogs into the ZIM SDK within the subscriptionDuration, the subscription remains valid, and there is no need to call the subscription method again.
Untitled
ZIMUserStatusSubscribeConfig *config = [[ZIMUserStatusSubscribeConfig alloc] init];
config.subscriptionDuration = 60; // Subscription duration (in minutes), valid range is 1 to 43200 (30 days)
[zim subscribeUsersStatus:@[@"userIdA",@"userIdB"]
                   config:config
                 callback:^(NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
                            ZIMError *_Nonnull errorInfo) {
                for(ZIMErrorUserInfo *errorUserInfo in errorUserList){
                    errorUserInfo.userID; // User ID for which subscription failed
                    errorUserInfo.reason; // Error code for subscription failure
                }
}];
1
Copied!

Result Callback

Subsequently, within the time frame specified by subscriptionDuration, updates to the target user's user status will be returned via the ZIMEventHandler event callback in userStatusUpdated.

Note

If a user logs in again after subscribing, the ZIM SDK will proactively notify the user of the last status change of their subscribed target user during the logout period through this callback.

Untitled
@method ZIMEventHandlerImpl : NSObject<ZIMEventHandler>

+(ZIMEventHandlerImpl *)getInstance();

@end

@implementation ZIMEventHandlerImpl

- (void)zim:(ZIM *)zim userStatusUpdated:(NSArray<ZIMUserStatus *> *)userStatusList {
    for(ZIMUserStatus *userStatus in userStatusList){
        userStatus.userID; // Target user ID
        userStatus.onlineStatus; // User online status
        userStatus.customStatus; // User custom status
        userStatus.onlinePlatforms; // User online platform list
        userStatus.lastUpdateTime; // Last update time of user status
        userStatus.lastOnlineStatusUpdateTime; // Last change time of user online status
        userStatus.lastCustomStatusUpdateTime; // Last change time of user custom status
    }
}

// Other callback events..

@end

ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];

[zim setEventHandler: eventHandlerImpl];
1
Copied!

Unsubscribe from User Status

If you no longer wish to monitor the user status of certain users, you can call the unsubscribeUsersStatus interface and pass the target user IDs (up to 100 users) into the userIDs parameter.

Untitled
[zim unsubscribeUserStatus:@[@"userIdA",@"userIdB"]
                  callback:^(NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
                                ZIMError *_Nonnull errorInfo) {
        for(ZIMErrorUserInfo *errorUserInfo in errorUserList){
            errorUserInfo.userID; // User ID of the failed unsubscription
            errorUserInfo.reason; // Error code of the failed unsubscription
        }
}];
1
Copied!

Query user status

If you only need to obtain the user status of a target user once without continuous attention, you can call the queryUsersStatusByUserIDs interface and pass in the userIDs of the target users (up to 200 users, excluding the current user).

Untitled
[zim queryUsersStatusByUserIDs:userIDs
                      callback:^(NSArray<ZIMUserStatus *> *_Nonnull userStatusList,
                            NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
                            ZIMError *_Nonnull errorInfo) {
    for(ZIMUserStatus *userStatus in userStatusList){
        userStatus.userID; // Target user ID
        userStatus.onlineStatus; // User's online status
        userStatus.onlinePlatforms; // List of platforms where the user is online
        userStatus.lastUpdateTime; // Last update time of the online status
    }
    for(ZIMErrorUserInfo *errorUserInfo in errorUserList){
        errorUserInfo.userID; // User ID for which the query failed
        errorUserInfo.reason; // Error code for the failed query
    }
}];
1
Copied!

Query the user status subscription list

If a user wants to know which users' user statuses they have subscribed to, they can use the querySubscribedUserStatusListWithConfig interface. By passing an empty config.userIDs parameter, the user can obtain the complete subscription list, including the current status of the subscribed target users and the duration of the subscription validity.

If a user wants to confirm whether they have subscribed to certain users' user statuses, they can pass the userID of the query target into the config.userIDs parameter.

Untitled
ZIMSubscribedUserStatusQueryConfig *config = [[ZIMSubscribedUserStatusQueryConfig alloc] init];
// The target user IDs to query (a maximum of 200 users per query)
// When userIDs is empty, it indicates retrieving the complete subscription table information
// When userIDs is not empty, it indicates checking whether the target users are in the subscription list
// If they are, the result callback will include their status information
// If they are not, the result callback will not include any related information
config.userIDs = @[@"userIdA",@"userIdB"];

[zim
    querySubscribedUserStatusListWithConfig:config
                                    callback:^(NSArray<ZIMUserStatusSubscription *>
                                        *_Nonnull userStatusSubscriptionList,
                                    ZIMError *_Nonnull errorInfo) {

}];
1
Copied!

Monitoring the online status of the current user on the current device

If you need to monitor the online status of the current user on the current device, you can obtain the user's current connection status ( ZIMConnectionState ) via the connectionStateChanged callback and determine the online status of the current user based on it.

In ZIMConnectionState, ZIMConnectionStateConnected indicates that the user is online, and ZIMConnectionStateDisconnected indicates that the user is offline. For the remaining two connection statuses, ZIMConnectionStateConnecting and ZIMConnectionStateReconnecting, you can decide whether the user is online based on your business logic.

Untitled
@interface ZIMEventHandlerImpl : NSObject<ZIMEventHandler>

+(ZIMEventHandlerImpl *)getInstance;

@end

@implementation ZIMEventHandlerImpl

- (void)connectionStateChanged:(ZIMConnectionState)state event:(ZIMConnectionEvent)event extendedData:(NSDictionary *)extendedData{
    switch (state) {
        case ZIMConnectionStateConnected:
            // Connected, you can map the current user's online status as online
            break;
        case ZIMConnectionStateConnecting:
            // Connecting, based on your business logic, you can map the current user's online status as either online or offline
            break;
        case ZIMConnectionStateReconnecting:
            // Reconnecting, based on your business logic, you can map the current user's online status as either online or offline
            break;
        case ZIMConnectionStateDisconnected:
            // Disconnected, you can map the current user's online status as offline
            break;
        default:
            break;
    }
}

// Other callback events..

@end

ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];

[zim setEventHandler: eventHandlerImpl];
1
Copied!

Listening to the List of Online Platforms for the Current User

In a multi-device login scenario, when a user logs in from a second platform, you can listen to the userStatusUpdated callback to learn about currently online platforms via the userStatus.onlinePlatforms property. Developers can use this information to display the user's online status across different platforms (e.g., showing that the user is online on iOS and Windows).

Note

When the onUserStatusUpdated callback returns the userStatus information for the current user, the onlineStatus will be unknown, and lastUpdateTime will be 0, which does not accurately reflect the online status.

Untitled
@interface ZIMEventHandlerImpl : NSObject<ZIMEventHandler>

+(ZIMEventHandlerImpl *)getInstance;

@end

@implementation ZIMEventHandlerImpl

- (void)zim:(ZIM *)zim userStatusUpdated:(NSArray<ZIMUserStatus *> *)userStatusList {
    for(ZIMUserStatus *userStatus in userStatusList){
        if([userStatus.userID isEqual:myUserID]){
            userStatus.onlineStatus; // For the current user, onlineStatus will be UNKNOWN
            userStatus.onlinePlatforms; // Online platforms list of the current user
            userStatus.lastUpdateTime;  // For the current user, lastUpdateTime will be 0
        }
    }
}

// Other callback events..

@end

ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];

[zim setEventHandler: eventHandlerImpl];
1
Copied!

Previous

Friend management

Next

Manage rooms