Subscribe to user status
Subscribe to the online status of visible users with OpenIM iOS SDK.
Subscribe to target users' online presence and multi-device connection statuses using subscribeUsersStatus:onSuccess:onFailure:. Once subscribed, the client receives real-time updates when those users log in, disconnect, or change active platforms.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
userIDs | NSArray<NSString *> * | Yes | List of user IDs to subscribe to. Filter empty strings and duplicate entries, and subscribe exclusively to users currently visible on screen. |
Code example
[[OIMManager manager] subscribeUsersStatus:userIDs
onSuccess:^(NSArray<OIMUserStatusInfo *> *states) {
[presenceStore mergeStates:states];
}
onFailure:^(NSInteger code, NSString *message) {
NSLog(@"subscribeUsersStatus failed: %ld %@", (long)code, message);
}];When users scroll out of view or when leaving the screen, invoke Unsubscribe from user status to release subscription quotas.
Return value
The onSuccess callback delivers an array of OIMUserStatusInfo objects capturing the initial presence state and active client platforms for all successfully subscribed users.
State changes after the call
- Local state merge: Merge presence records into local UI collections using
userIDas the unique entity key. - Asynchronous event listening: Receive ongoing presence updates via the
onUserStatusChanged:delegate inOIMUserListener. - Re-query calibration: To refresh snapshots of active subscriptions, invoke Refresh subscribed user status.
@interface PresenceStore () <OIMUserListener>
@end
@implementation PresenceStore
- (void)startListening {
[[OIMManager callbacker] addUserListener:self];
}
- (void)stopListening {
[[OIMManager callbacker] removeUserListener:self];
}
- (void)onUserStatusChanged:(OIMUserStatusInfo *)status {
[self updateUserStatus:status];
}
@endRetain this listener instance strongly, and invoke removeUserListener: when logging out or switching user accounts.
Was this page helpful?