Skip to main content

Friend management

This guide covers adding, removing, and managing friends, handling friend requests, and setting friend info.

Friend event listener

Register a UserHandler after SDK initialization and before connecting to receive friend-related events. Swift is the primary example below, followed by the Objective-C equivalent.

swift
import NexconnChatSDK

final class FriendService: NSObject, UserHandler {
func start() {
NCEngine.addUserHandler(identifier: "FRIEND_HANDLER", handler: self)
}

func stop() {
NCEngine.removeUserHandler(forIdentifier: "FRIEND_HANDLER")
}

func onFriendAdd(_ event: FriendAddEvent) {
print("Friend added: \(event.userId)")
}

func onFriendDelete(_ event: FriendDeleteEvent) {
print("Friends deleted: \(event.userIds)")
}

func onFriendApplicationStatusChanged(_ event: FriendApplicationStatusChangedEvent) {
print("Friend request status changed: \(event.userId), \(event.applicationStatus.rawValue)")
}

func onFriendCleared(_ event: FriendClearedEvent) {
print("All friends cleared at: \(event.operationTime)")
}

func onFriendInfoChangedSync(_ event: FriendInfoChangedSyncEvent) {
print("Friend info updated: \(event.userId)")
}
}
tip

All friend management APIs are on NCEngine.userModule.

The same UserHandler callbacks also deliver friend profile changes and application-state changes on the current device.

Friend operations

Add a friend

tip

Max 3,000 friends per user.

swift
import NexconnChatSDK

let params = AddFriendParams(userId: "userId1")
params.extra = "Friend request message"

NCEngine.userModule.addFriend(params: params) { processCode, error in
guard error == nil else {
print("Failed to add friend: \(error!.localizedDescription)")
return
}

switch processCode {
case 0:
print("Friend added directly.")
case 25461:
print("Friend request sent and waiting for approval.")
default:
print("Friend request finished with process code: \(processCode)")
}
}

Set friend request permission

Use setFriendAddPermission(_:completion:) to define how other users can add the current user:

swift
import NexconnChatSDK

NCEngine.userModule.setFriendAddPermission(.needVerify) { error in
if error == nil {
print("Friend request permission updated.")
}
}
ValueDescription
NCFriendAddPermissionFreeAnyone can add as friend
NCFriendAddPermissionNeedVerifyApproval required (default)
NCFriendAddPermissionNoOneAllowedNo one can add

Remove friends

Remove up to 100 friends per call:

swift
import NexconnChatSDK

NCEngine.userModule.deleteFriends(userIds: ["userId1", "userId2"]) { error in
if error == nil {
print("Friends removed.")
}
}

Check friendship

Call checkFriends(userIds:completion:) to batch query friendship relationships:

swift
import NexconnChatSDK

NCEngine.userModule.checkFriends(userIds: ["userId1"]) { relations, error in
guard let relations, error == nil else {
print("Failed to check friendship: \(error?.localizedDescription ?? "unknown error")")
return
}

for relation in relations {
print("Friend relation: \(relation.userId), \(relation.relation.rawValue)")
}
}

Friend requests

Get friend request list (paginated)

Requests expire after 7 days. In Swift, use UserModule.createFriendApplicationsQuery(params:) to create a paginated query:

swift
import NexconnChatSDK

let params = FriendApplicationsQueryParams()
params.pageSize = 20

let query = UserModule.createFriendApplicationsQuery(params: params)
query.loadNextPage { page, error in
guard let applications = page?.data, error == nil else {
print("Failed to load friend applications: \(error?.localizedDescription ?? "unknown error")")
return
}

for application in applications {
print("Friend application: \(application.userId), \(application.applicationStatus.rawValue)")
}
}

Accept a friend request

swift
import NexconnChatSDK

NCEngine.userModule.acceptFriendApplication(userId: "userId1") { error in
if error == nil {
print("Friend request accepted.")
}
}

Decline a friend request

swift
import NexconnChatSDK

NCEngine.userModule.refuseFriendApplication(userId: "userId1") { error in
if error == nil {
print("Friend request declined.")
}
}

Friend info

Get friend list

swift
import NexconnChatSDK

NCEngine.userModule.getFriends { friendInfos, error in
guard let friendInfos, error == nil else {
print("Failed to load friend list: \(error?.localizedDescription ?? "unknown error")")
return
}

print("Friend count: \(friendInfos.count)")
}

Set friend info

tip

Custom extension keys must be configured in the Console first. Max 10 extension keys.

swift
import NexconnChatSDK

let params = SetFriendInfoParams(userId: "userId1")
params.remark = "Nickname"
params.extProfile = ["key1": "value1"]

NCEngine.userModule.setFriendInfo(params: params) { errorKeys, error in
if let error {
print("Failed to set friend info: \(error.localizedDescription), fields: \(errorKeys ?? [])")
return
}

print("Friend info updated.")
}

Get friend info by user ID

Query up to 100 users per call:

swift
import NexconnChatSDK

NCEngine.userModule.getFriendsInfo(userIds: ["userId1", "userId2"]) { friendInfos, error in
guard let friendInfos, error == nil else {
print("Failed to load friend info: \(error?.localizedDescription ?? "unknown error")")
return
}

print("Friend info count: \(friendInfos.count)")
}

Search friends by name

Matches friend remark first, then friend name:

swift
import NexconnChatSDK

NCEngine.userModule.searchFriendsInfo(name: "searchQuery") { friendInfos, error in
guard let friendInfos, error == nil else {
print("Search failed: \(error?.localizedDescription ?? "unknown error")")
return
}

print("Matched friends: \(friendInfos.count)")
}