logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

Manage groups


ZEGOCLOUD's In-app Chat (the ZIM SDK) provides the capability of group management, allowing you to create a group, ungroup, join and leave a group, and maintain the group relation chain.

With the group management feature, you can create different types of group chats as needed, such as colleague groups, social groups, fan groups, and more.

Create a group

After user A logs in to the ZIM SDK on a client, the user can call the createGroup method to create a group with advanced settings. In this case, user A logged on this client is the group owner, and users on other clients can join this group using the groupID.

You can check whether the group is created through the ZIMGroupCreatedCallback callback. For more information about error codes, see Error codes.

Warning
  • You can specify custom rules for groupID generation. A group ID can contain only digits, letters, and the following characters: '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~', and cannot start with a pound sign (#). If this parameter is empty, the ZIM server automatically generates a value. We recommend that you set a meaningful group ID generation rule. You can associate the group ID with your business account system.
  • If a user calls the createGroup method to create a group, the user automatically joins the group without calling the joinGroup method.
  • The user creating a group is the group owner. For more information about group ownership transfer, see Transfer the group ownership.
Untitled
// Create a group.
// The groupID parameter can contain up to 32 bytes in length, including digits, letters, and the following characters: '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'. It cannot start with a pound sign (#).
// The groupName parameter can contain up to 50 bytes in length, with no limit on characters.
ZIMGroupInfo *groupInfo = [[ZIMGroupInfo alloc] init];
groupInfo.groupID = @"groupID";
groupInfo.groupName = @"groupName";
groupInfo.groupAvatarUrl = @"groupAvatarUrl";

ZIMGroupAdvancedConfig *advancedConfig = [[ZIMGroupAdvancedConfig alloc] init];
// The mode where a user sends a group joining application.
advancedConfig.joinMode = ZIMGroupJoinModeAny;
// The mode where a group member invites a user to a group.
advancedConfig.inviteMode = ZIMGroupInviteModeAny;
// The mode where a user is invited to a group.
advancedConfig.beInviteMode = ZIMGroupBeInviteModeNone;

// The maximum number of members in the group.
advancedConfig.maxMemberCount = 300;

[[ZIM getInstance] createGroup:groupInfo userIDs:@[@"userID1",@"userID2"] config:advancedConfig callback:^(ZIMGroupFullInfo * _Nonnull groupInfo, NSArray<ZIMGroupMemberInfo *> * _Nonnull userList, NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
    // The business logic after the group is created.
}];
1
Copied!

Join a group

Note

If you want users to automatically obtain group history messages after joining a group, please contact the ZEGOCLOUD technical support team for configuration.

After logging in to the ZIM SDK, users can apply to join or be invited to join a group created by user A.

If a user successfully joins the group, all group members (including the user) receive the groupMemberStateChanged and groupStateChanged callbacks.

Untitled
- (void)zim:(ZIM *)zim
    groupMemberStateChanged:(ZIMGroupMemberState)state
                      event:(ZIMGroupMemberEvent)event
                   userList:(NSArray<ZIMGroupMemberInfo *> *)userList
               operatedInfo:(ZIMGroupOperatedInfo *)operatedInfo
    groupID:(NSString *)groupID{
}
- (void)zim:(ZIM *)zim
    groupStateChanged:(ZIMGroupState)state
                event:(ZIMGroupEvent)event
         operatedInfo:(ZIMGroupOperatedInfo *)operatedInfo
  groupInfo:(ZIMGroupFullInfo *)groupInfo{
}
1
Copied!

Method 1: Apply to join a group

The user calls the corresponding method to apply to join a group based on the joinMode of the group.

  • If the joinMode is set to 0 (ANY), the user calls the joinGroup method and passes in the groupID to join the group without approval. If the groupID does not exist, the operation fails.
Untitled
// Join the group on another client.
[zim joinGroup:GroupID callback:^(ZIMGroupFullInfo * _Nonnull groupInfo, ZIMError * _Nonnull errorInfo) {
    // The business logic after the joinGroup method is called.
}];
1
Copied!
  • If the joinMode is set to 1 (AUTH):
  1. The user calls the sendGroupJoinApplicationToGroupID method to send an application.
Untitled
ZIMGroupJoinApplicationSendConfig *config = [[ZIMGroupJoinApplicationSendConfig alloc] init];
config.wording = @"Let me join the group.";
[zim sendGroupJoinApplicationToGroupID:groupID config:config callback:^(NSString * _Nonnull groupID, ZIMError *errorInfo) {        
      // The callback for sending the group joining application.     
}];
1
Copied!
  1. The group owner or administrator receives the application notification by listening for the groupApplicationListChanged event.
Untitled
// Listen for groupApplicationListChanged event.
- (void)zim:(ZIM *)zim
groupApplicationListChanged:(NSArray<ZIMGroupApplicationInfo *> *)applicationList
      action:(ZIMGroupApplicationListChangeAction)action{
}      
1
Copied!
  1. The group owner or administrator handles the application.
Untitled
// Approve the application.
[zim acceptGroupJoinApplicationFromUserID:userID groupID:groupID config:config callback:^(NSString * _Nonnull groupID, NSString * _Nonnull userID, ZIMError * _Nonnull errorInfo) {
        // The callback for application approval.
}];
1
Copied!
Untitled
// Reject the application.
[zim rejectGroupJoinApplicationFromUserID:userID groupID:groupID config:config callback:^(NSString * _Nonnull groupID, NSString * _Nonnull userID, ZIMError * _Nonnull errorInfo) {
    // The callback for application rejection.
}];
1
Copied!
  1. The user applying to join the group, the group owner or administrator, and the method caller receive the groupApplicationUpdated callback.
Untitled
// Listen for groupApplicationUpdated event.
- (void)zim:(ZIM *)zim groupApplicationUpdated:(NSArray<ZIMGroupApplicationInfo *> *)applicationList{

}
1
Copied!

Method 2: Invite a user to a group

  1. A group member can invite a user to the group by calling one of the following methods.
Warning

Make sure that the invitee has been registered by calling the loginWithUserID method; otherwise, the operation fails.

Untitled
// A group member invites a user to the group.
[zim inviteUsersIntoGroup:userIDs groupID:groupID callback:^(NSArray<ZIMGroupMemberInfo *> * _Nonnull userList, NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
    // The business logic after the inviteUsersIntoGroup method is called.
}];
1
Copied!
Untitled
// Send the group invitation.
ZIMGroupInviteApplicationSendConfig *config = [[ZIMGroupInviteApplicationSendConfig alloc] init];
config.wording = @"xxx invited you to join the group.";
[zim sendGroupInviteApplicationsToUserIDs:@[@"userID1",@"userID2"] groupID:@"groupID" config:config callback:^(NSString * _Nonnull groupID, NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {

}];   
1
Copied!

The following table describes the operation results of different methods called by different roles in a group in inviteMode and beInviteMode modes.

beInviteModeinviteModeMethodCallerResult
0: None0: AnyinviteUsersIntoGroupAll group membersThe invitee joins the group without approval.
sendGroupInviteApplicationsToUserIDsThe invitee joins the group without approval, with no application generated.
1: AdmininviteUsersIntoGroupOrdinary membersFailed.
Group owner or administratorThe invitee joins the group without approval.
sendGroupInviteApplicationsToUserIDsOrdinary membersFailed.
Group owner or administratorThe invitee joins the group without approval, with no application generated.
1: Auth0: AnyinviteUsersIntoGroupAll group membersAn application is generated and sent to the invitee for approval.
sendGroupInviteApplicationsToUserIDsAn application is generated and sent to the invitee for approval. If the caller is the group owner or administrator, the groupApplicationListChanged callback is generated.
1: AdmininviteUsersIntoGroupOrdinary membersFailed.
Group owner or administratorAn application is generated and sent to the invitee for approval.
sendGrsendGroupInviteApplicationsToUserIDsoupInviteApplicationsOrdinary membersFailed.
Group owner or administratorAn application is generated and sent to the invitee for approval. If the caller is the group owner or administrator, the groupApplicationListChanged callback is generated.
  1. If the beInviteMode is set to 1 (AUTH), the invitee and inviter (the group owner or administrator) receive the notification in the groupApplicationListChanged callback. The invitee can perform the following operations on the application:
Untitled
[zim acceptGroupInviteApplicationFromInviterUserID:inviterUserID groupID:groupID config:config callback:^(ZIMGroupFullInfo * _Nonnull groupInfo, NSString * _Nonnull inviterUserID, ZIMError * _Nonnull errorInfo) {

}];
1
Copied!
Untitled
[zim rejectGroupInviteApplicationFromInviterUserID:inviterUserID groupID:groupID config:config callback:^(NSString * _Nonnull groupID, NSString * _Nonnull inviterUserID, ZIMError * _Nonnull errorInfo) {

}];
1
Copied!
  1. If the beInviteMode is set to 1 (AUTH), the invitee and inviter (the group owner or administrator) receive the approval notification in the groupApplicationUpdated callback.
Untitled
- (void)zim:(ZIM *)zim groupApplicationUpdated:(NSArray<ZIMGroupApplicationInfo *> *)applicationList{
        
}
1
Copied!

Leave a group

A group member can leave a group or be removed from a group.

Warning

After a group member leaves a group, the local list of conversations is retained, and historical group messages can be viewed.

Method 1: A group member leaves the group

After logging in to the ZIM SDK, the group member calls the leaveGroup method and passes in the groupID. If the groupID does not exist, the operation fails. After the group member leaves the group, all group members receive the groupMemberStateChanged callback.

Note

If the group owner leaves the group, the group ownership is automatically transferred to the first group member. If all group members leave the group, the group is automatically disbanded.

Untitled
// Leave a group.
[zim leaveGroup:groupID callback:^(ZIMError * _Nonnull errorInfo) {
        // The business logic after the leaveGroup method is called.
}];
1
Copied!

Method 2: The group owner removes a group member from the group

The group owner calls the kickGroupMembers method and passes in the groupID and userIDList (list of users to be removed). If the groupID does not exist, the operation fails. After one or more group members are removed, all group members (including the group owner and the removed group members) receive the groupMemberStateChanged callback.

Warning
  • Only the group owner or administrator can call the kickGroupMembers method to remove a group member, who does not need to be logged in or approve the removal.
  • The userID of the user to be removed must be in the list of group members; otherwise, the operation fails.
Untitled
// The group owner removes one or more group members from the group.
[zim kickGroupMembers:userIDs groupID:groupID callback:^(NSArray<NSString *> * _Nonnull kickedUserIDList, NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
        // The business logic after the group owner calls the kickGroupMembers method. 
}];
1
Copied!

Disband a group

After logging in to the ZIM SDK, the group owner calls the dismissGroup method to disband a group. After the group is disbanded, all group members receive the groupStateChanged callback.

Note
  • Only the group owner can call the dismissGroup method to disband a group.
  • If all group members leave a group, the group is automatically disbanded.
  • After a group is disbanded, the local list of conversations is retained, and historical messages can be viewed.
Untitled
// The group owner disbands the group.
[zim dismissGroup:groupID callback:^(ZIMError * _Nonnull errorInfo) {
        // The business logic after the dismissGroup method is called.
 }];
1
Copied!

More features

Query the list of joined groups

After logging in to the ZIM SDK, a user calls the queryGroupList method to query the list of joined groups.

Untitled
// The user queries the list of joined groups.
[zim queryGroupListByGroupID:^(NSArray<ZIMGroupInfo *> * _Nonnull groupList, ZIMError * _Nonnull errorInfo) {
    // The business logic after the queryGroupListByGroupID method is called.
}];
1
Copied!

Search for joined groups

After logging in to the ZIM SDK, a user calls the searchLocalGroupsWithConfig method and passes in the config and callback parameters to search for joined groups by condition.

The search result is returned in the ZIMGroupsSearchedCallback callback.

Untitled
// Search for joined groups that contain a member named "zego".
ZIMGroupSearchConfig *config = [[ZIMGroupSearchConfig alloc] init];
config.count = 10;
config.nextFlag = 0;
config.isAlsoMatchGroupMemberNickname = true; // If the nickname of a group member contains "zego", the group is returned in the search result.
config.isAlsoMatchGroupMemberUserName = true; // If the username of a group member contains "zego", the group is returned in the search result.
config.keywords = @[@"zego"];
[[ZIM getInstance] searchLocalGroupsWithConfig:config callback:^(NSArray<ZIMGroupSearchInfo *> * _Nonnull groupSearchInfoList, unsigned int nextFlag, ZIMError * _Nonnull errorInfo) {
    if(errorInfo.code == ZIMErrorCodeSuccess){
        // You can obtain the group information in the groupSearchInfoList field.
    }else{
        // Handle the error based on the error code table.
    }
}];
1
Copied!

Mute or unmute a group

If a group is muted, group members cannot send messages to the group.

After logging in to the ZIM SDK, a user can mute or unmute a group on which the user has management permission by calling the muteGroup method and passing in parameters to specify the group ID, mute mode, duration, and role.

The ZIM SDK supports three mute modes:

  • All group members are muted.
  • All ordinary group members (whose role value is 3) are muted.
  • Group members of specified roles are muted.

The result is returned in the ZIMGroupMutedCallback callback.

Note

If you want to block specific group members from posting, please refer to Set mute status for group members - Manage group members.

Untitled
// Configure group muting.
ZIMGroupMuteConfig *muteConfig = [[ZIMGroupMuteConfig alloc] init];
// Group members of specified roles are muted.
muteConfig.mode = ZIMGroupMuteModeCustom;
// These members are muted for 30 seconds.
muteConfig.duration = 30;
// Group members whose `role` value is `3` or `5` are muted.
muteConfig.roles = @[@3,@5];

[[ZIM getInstance] muteGroup:YES groupID:@"groupID" config:muteConfig callback:^(NSString * _Nonnull groupID, BOOL isMute, ZIMGroupMuteInfo * _Nonnull info, ZIMError * _Nonnull errorInfo) {
    // You can obtain the group muting information in the `info` field.
}];
1
Copied!

After a group is muted or unmuted, all group members receive the groupMutedInfoUpdated callback and know which roles are muted or unmuted.

Untitled

- (void)zim:(ZIM *)zim
    groupMutedInfoUpdated:(ZIMGroupMuteInfo *)muteInfo
            operatedInfo:(ZIMGroupOperatedInfo *)operatedInfo
                 groupID:(NSString *)groupID{
    // Listen for the group muting status change and handle the change by inheriting the ZIMEventHandler method.
}
1
Copied!

Check whether a user is in a group

To check whether a user is in a group, call any of the following methods:

For group chats, the result is indicated by the isDisabled property of ZIMGroupConversation.

Valid values of isDisabled:

  • true: The user is not in the group. If the user leaves the group or is removed from the group, or the group is disbanded, the value of isDisabled is false.
  • false: The user is in the group.

Update the group joining mode

After logging in to the ZIM SDK, the group owner or administrator calls the updateGroupJoinMode method to update the group joining mode.

After the mode is updated, all group members receive the groupVerifyInfoUpdated callback.

Untitled
// Listen for groupVerifyInfoUpdated event.
- (void)zim:(ZIM *)zim
    groupVerifyInfoUpdated:(ZIMGroupVerifyInfo *)verifyInfo
            operatedInfo:(ZIMGroupOperatedInfo *)operatedInfo
    groupID:(NSString *)groupID{

}

// Update joinMode of a group.
[zim updateGroupJoinMode:ZIMGroupJoinModeAuth groupID:@"groupID" callback:^(NSString * _Nonnull groupID, ZIMGroupJoinMode mode, ZIMError * _Nonnull errorInfo) {

}];
1
Copied!

Update inviteMode of a group

After logging in to the ZIM SDK, the group owner or administrator calls the updateGroupInviteMode method to update inviteMode of the group.

After the mode is updated, all group members receive the groupVerifyInfoUpdated callback.

Untitled
// Listen for onGroupVerifyInfoUpdated event.
- (void)zim:(ZIM *)zim
    groupVerifyInfoUpdated:(ZIMGroupVerifyInfo *)verifyInfo
              operatedInfo:(ZIMGroupOperatedInfo *)operatedInfo
    groupID:(NSString *)groupID{

}

// Update inviteMode of the group.
[zim updateGroupInviteMode:ZIMGroupInviteModeNone groupID:@"groupID" callback:^(NSString * _Nonnull groupID, ZIMGroupInviteMode mode, ZIMError * _Nonnull errorInfo) {

}];
1
Copied!

Update beInviteMode

After logging in to the ZIM SDK, the group owner or administrator calls the updateGroupBeInviteMode method to update beInviteMode.

After the mode is updated, all group members receive the groupVerifyInfoUpdated callback.

Untitled
// Listen for onGroupVerifyInfoUpdated event.
- (void)zim:(ZIM *)zim
    groupVerifyInfoUpdated:(ZIMGroupVerifyInfo *)verifyInfo
              operatedInfo:(ZIMGroupOperatedInfo *)operatedInfo
    groupID:(NSString *)groupID{
    
    }

// Update beInviteMode.
  [zim updateGroupBeInviteMode:ZIMGroupBeInviteModeAuth groupID:@"groupID" callback:^(NSString * _Nonnull groupID, ZIMGroupBeInviteMode mode, ZIMError * _Nonnull errorInfo) {

  }];
1
Copied!

Query the list of group joining applications

After logging in to the ZIM SDK, a user calls the queryGroupApplicationListWithConfig method to query the list of group joining applications. The query result contains applications sent to the user.from and to the user.

Untitled
ZIMGroupApplicationListQueryConfig *config = [[ZIMGroupApplicationListQueryConfig alloc] init];
config.count = 20;
[zim queryGroupApplicationListWithConfig:config callback:^(NSArray<ZIMGroupApplicationInfo *> * _Nonnull applicationList, unsigned int nextFlag, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

Previous

Manage room user attributes

Next

Manage group info