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

Friend management


Overview

ZIM SDK supports friend management, allowing users to directly add and delete friends, view their friends list, send friend requests to other users, accept or reject friend requests, view friend request lists, check their friend relationship with other users, and query or modify friend information.

Each user can have a maximum of 3000 friends.

Basic functions

By calling the setEventHandler API, you can register callbacks to listen for friend-related events, including: friendListChanged, friendApplicationListChanged, friendApplicationUpdated and friendInfoUpdated.

Untitled
// Callback for friend list changes
- (void)zim:(ZIM *)zim
    friendListChanged:(NSArray<ZIMFriendInfo *> *)friendInfoList
               action:(ZIMFriendListChangeAction)action{
    // todo
}

// Callback for friend info updates
- (void)zim:(ZIM *)zim friendInfoUpdated:(NSArray<ZIMFriendInfo *> *)friendInfoList{
    // todo
}

// Callback for friend application list changes
- (void)zim:(ZIM *)zim
    friendApplicationListChanged:(NSArray<ZIMFriendApplicationInfo *> *)friendApplicationInfoList
                          action:(ZIMFriendApplicationListChangeAction)action{
    // todo
}

// Callback for friend application info updates
- (void)zim:(ZIM *)zim
    friendApplicationUpdated:(NSArray<ZIMFriendApplicationInfo *> *)friendApplicationInfoList{
    // todo
}
1
Copied!

Add friends directly

After logging in to the ZIM SDK, users can use the addFriendByUserID API to directly add other users as friends without requiring their consent. This allows users to set friend remarks and attributes for the added user.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of adding a friend is returned through the ZIMFriendAddedCallback .

Untitled
// Add friend directly
ZIMFriendAddConfig *addConfig = [[ZIMFriendAddConfig alloc] init];
addConfig.wording = @"hello";
addConfig.friendAlias = @"Mark";
addConfig.friendAttributes = @{@"k0":@"SZ"};
[[ZIM getInstance] addFriendByUserID:@"userID" config:addConfig callback:^(ZIMFriendInfo * _Nonnull friendInfo, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

After successfully adding a friend, the related users will receive the callback friendListChanged to notify them that the user has become a friend.

Batch deleting friends

After logging in to the ZIM SDK, users can use the deleteFriendsByUserIDs API to batch delete up to 20 friends in a one-way or two-way deletion manner.

In this example, one-way deletion and two-way deletion are explained.

  • One-way deletion: If user A deletes user B in a one-way manner, user B is no longer a friend of user A, but not vice versa.
  • Two-way deletion: If user A deletes user B in a two-way manner, they are no longer friends with each other.

The result of deleting friends is returned through ZIMFriendsDeletedCallback.

Untitled
// Batch delete friends
// type is ZIMFriendDeleteTypeBoth: bilateral deletion
// type is ZIMFriendDeleteTypeSingle: unilateral deletion
ZIMFriendDeleteConfig *friendDeleteConfig = [[ZIMFriendDeleteConfig alloc] init];
friendDeleteConfig.type = ZIMFriendDeleteTypeBoth;
[[ZIM getInstance] deleteFriendsByUserIDs:@[@"userID1",@"userID2"] config:friendDeleteConfig callback:^(NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

After successfully deleting a friend, depending on the deletion type, the related users will receive the callback friendListChanged to notify them that the user is no longer a friend.

Send friend requests

After logging in to the ZIM SDK, users can use the sendFriendApplicationToUserID API to send friend requests to other users and set friend remarks and attributes.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of sending a friend request is returned through ZIMFriendApplicationSentCallback.

Untitled
// Send friend application
ZIMFriendApplicationSendConfig *sendConfig = [[ZIMFriendApplicationSendConfig alloc] init];
sendConfig.wording = @"Hello!";
sendConfig.friendAlias = @"Mark";
sendConfig.friendAttributes = @{@"k0":@"SZ"};
[[ZIM getInstance] sendFriendApplicationToUserID:@"userID" config:sendConfig callback:^(ZIMFriendApplicationInfo * _Nonnull applicationInfo, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

The target user will receive the friendApplicationListChanged callback, indicating that a user has requested to become friends. The user can choose to accept or reject the request within 7 days.

Note

If you need to adjust the friend application validity period, please contact the ZEGOCLOUD technical support team.

Accept friend requests

After logging in to ZIM SDK, users can call the acceptFriendApplicationFromUserID API, passing the ID of the user who initiated the request, to accept a friend request.

The result of accepting a friend request is returned through ZIMFriendApplicationAcceptedCallback.

Untitled
// Accept friend application
ZIMFriendApplicationAcceptConfig *acceptConfig = [[ ZIMFriendApplicationAcceptConfig alloc] init];
acceptConfig.friendAlias = @"Mark";
acceptConfig.friendAttributes = @{@"k0":@"SZ"};
[[ZIM getInstance] acceptFriendApplicationFromUserID:@"userID" config:acceptConfig callback:^(ZIMFriendInfo * _Nonnull friendInfo, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

Both the requesting user and the requested user will not only receive the friendApplicationUpdated to know that the other user has become their friend.

Reject friend requests

After logging in to ZIM SDK, users can reject friend requests by using the rejectFriendApplicationFromUserID API.

The result of rejecting a friend request is returned through ZIMFriendApplicationRejectedCallback.

Untitled
// Reject friend application
ZIMFriendApplicationRejectConfig *rejectConfig = [[ZIMFriendApplicationRejectConfig alloc] init];
[[ZIM getInstance] rejectFriendApplicationFromUserID:@"userID" config:rejectConfig callback:^(ZIMUserInfo * _Nonnull userInfo, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

Both the requesting user and the requested user will receive the friendApplicationUpdated callback to know that the application has been rejected.

Query friend list

After logging in to the ZIM SDK, users can use the queryFriendListWithConfig API to retrieve the paginated complete friends list.

The query results are returned through ZIMFriendListQueriedCallback, and the returned friends list is sorted in descending order based on the time of friend relationship creation.

Untitled
// Query the list of friend information
ZIMFriendListQueryConfig *queryConfig = [[ ZIMFriendListQueryConfig alloc] init];
queryConfig.count = 100;
queryConfig.nextFlag = 0;
[[ZIM getInstance] queryFriendListWithConfig:queryConfig callback:^(NSArray<ZIMFriendInfo *> * _Nonnull friendList, unsigned int nextFlag, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

Query friend request list

After logging in to the ZIM SDK, users can use the queryFriendApplicationListWithConfig API to retrieve the friend request list and understand the status of each request. The friend request list includes both the requests initiated by the user to other users and the requests initiated by other users to the user. The query results are returned through ZIMFriendApplicationListQueriedCallback.

Untitled
// Query friend application information list
ZIMFriendApplicationListQueryConfig *queryConfig = [[ZIMFriendApplicationListQueryConfig alloc] init];
queryConfig.count = 100;
queryConfig.nextFlag = 0;
[[ZIM getInstance] queryFriendApplicationListWithConfig:queryConfig callback:^(NSArray<ZIMFriendApplicationInfo *> * _Nonnull applicationList, unsigned int nextFlag, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

More functions

Update friend alias

After logging in to the ZIM SDK, users can update the alias they have for a friend using the updateFriendAlias API.

The result of the update is returned through ZIMFriendAliasUpdatedCallback.

Untitled
// Update friend alias
[[ZIM getInstance] updateFriendAlias:@"New Alias" userID:@"userID" callback:^(ZIMFriendInfo * _Nonnull friendInfo, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

After a successful update, the user can receive the friendInfoUpdated callback to know that the friend's information has been updated.

Update friend attributes

After logging in to the ZIM SDK, users can update friend attributes using the updateFriendAttributes API.

Note

You can set up to 5 friend attributes, and the corresponding key values must be k0, k1, k2, k3, and k4. It is recommended that you agree in advance on the actual meanings of each attribute and maintain consistency.

The result of updating friend attributes is returned through ZIMFriendAttributesUpdatedCallback.

Untitled
// Update friend attributes
[[ZIM getInstance] updateFriendAttributes:@{@"k1":@"v1",@"k2":@"v2"} userID:@"userID" callback:^(ZIMFriendInfo * _Nonnull friendInfo, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

After logging in to the ZIM SDK, users can update friend attributes using the friendInfoUpdated API.

Check friend relationships

After logging in to the ZIM SDK, users can use the checkFriendsRelationByUserID API to batch check their friend relationships with up to 20 other users.

ZEGOCLOUD allows one-way or two-way check of friendships. In this example, the friendship between users A and B is checked.

  • One-way check: Only checks whether user B is in the friend list of user A.
  • Two-way check: Checks whether users A and B are in the friend list of the other.

The result of checking friend relationships is returned through ZIMFriendsRelationCheckedCallback.

Untitled
// Check friend relationship with other users
// type is ZIMFriendRelationCheckTypeBoth: bidirectional
// type is ZIMFriendRelationCheckTypeSingle: unidirectional
ZIMFriendRelationCheckConfig *checkConfig = [[ZIMFriendRelationCheckConfig alloc] init];
checkConfig.type = ZIMFriendRelationCheckTypeSingle;
[[ZIM getInstance] checkFriendsRelationByUserIDs:@[@"userID1",@"userID2"] config:checkConfig callback:^(NSArray<ZIMFriendRelationInfo *> * _Nonnull relationInfos, NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

Batch query friend information

After logging in to the ZIM SDK, users can use the queryFriendsInfoByUserIDs API to batch query information for up to 20 friends.

The result of batch querying friend information is returned through ZIMFriendsInfoQueriedCallback.

Untitled
// Query friend information in batch
[[ZIM getInstance] queryFriendsInfoByUserIDs:@[@"userID1",@"userID2"] callback:^(NSArray<ZIMFriendInfo *> * _Nonnull friendInfos, NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

Search for friends

After logging in to the ZIM SDK, users can use the searchLocalFriendsWithConfig API to search for friends based on their usernames and aliases. Users can provide up to 5 keywords to search for friends who match all the provided keywords.

The search results are returned through ZIMFriendsSearchedCallback.

Untitled
ZIMFriendSearchConfig *searchConfig = [[ZIMFriendSearchConfig alloc] init];
searchConfig.count = 100;
searchConfig.keywords = @[@"a",@"b"];
searchConfig.isAlsoMatchFriendAlias = YES;
searchConfig.nextFlag = 0;
[[ZIM getInstance] searchLocalFriendsWithConfig:searchConfig callback:^(NSArray<ZIMFriendInfo *> * _Nonnull friendInfos, unsigned int nextFlag, ZIMError * _Nonnull errorInfo) {
    
}];
1
Copied!

Previous

Blacklist management

Next

Online Status Subscription