Browse SDKs · iOS
Platform
SDKsiOS

Block a user

Add a user to the current account’s blocklist with OpenIM iOS SDK.

Copy

Add a specified user to the authenticated account's blacklist using addToBlackList:ex:onSuccess:onFailure:.

Parameters

ParameterTypeRequiredDescription
userIDNSString *YesTarget user ID to blacklist.
exNSString *NoCustom extension string (e.g., JSON payload documenting reason or origin). Pass an empty string @"" when omitted.

Code example

[[OIMManager manager] addToBlackList:targetUserID
                                  ex:@"{\"source\":\"profile-menu\"}"
                           onSuccess:^(NSString *data) {
    [blacklistStore addUserID:targetUserID];
}
                           onFailure:^(NSInteger code, NSString *message) {
    NSLog(@"addToBlackList failed: %ld %@", (long)code, message);
}];

Blacklisting is a unilateral relationship established by the local user. It suppresses incoming messages and direct interactions from the blocked user; it is distinct from channel-level mutes or system-wide account bans.

Return value

The onSuccess callback signifies that OpenIMServer recorded the blacklist relationship.

State changes after the call

  • Local state merge: Insert or merge the newly blocked user into blacklist collections using userID as the unique entity key.
  • Asynchronous event listening: Linked devices observe blacklist changes in real time via onBlackAdded: in OIMFriendshipListener.
  • Re-query calibration: To retrieve a full snapshot of all blocked accounts, invoke Retrieve the blocklist.
@interface BlacklistStore () <OIMFriendshipListener>
@end

@implementation BlacklistStore

- (void)startListening {
    [[OIMManager callbacker] addFriendListener:self];
}

- (void)stopListening {
    [[OIMManager callbacker] removeFriendListener:self];
}

- (void)onBlackAdded:(OIMBlackInfo *)info {
    [self upsertBlackInfo:info];
}

@end

Retain this listener instance strongly, and invoke removeFriendListener: when signing out or switching accounts.