Quick Reference for AI Agents & Developers
Transfer ownership: CometChat.transferGroupOwnership(UID:GUID:onSuccess:onError:)
Permission: Only current group owner can transfer ownership
Note: Owner must transfer ownership before leaving the group
Related: Leave Group · Change Member Scope · Groups Overview
Transfer Group Ownership
In other words, as a logged-in user, how do I transfer the ownership of any group if I am the owner of the group?
In order to transfer the ownership of any group, the first condition is that you must be the owner of the group. In case you are the owner of the group, you can use the transferGroupOwnership() method.
The owner is not allowed to leave the group. If you as the owner would like to leave the group, you will have to transfer your ownership first to any other member of the group and only then you will be allowed to leave the group.
Method Parameters
Parameter Type Description UID String The UID of the new owner GUID String The GUID of the group
let uid = "cometchat-uid-2"
let guid = "cometchat-guid-1"
CometChat. transferGroupOwnership ( UID : uid, GUID : guid, onSuccess : { (success) in
print ( "Ownership transferred: \( success ) " )
}, onError : { (error) in
print ( "Error: \( error ? . errorDescription ) " )
})
Sample Payload - Transfer Group Ownership
Request Parameters: Parameter Type Description UID String Unique identifier of the new owner. Example: "cometchat-uid-2" GUID String Unique identifier of the group. Example: "cometchat-guid-1"
Success Response: Parameter Type Description success String Success message. Example: "Group ownership transferred successfully"
After Transfer: Effect Description New Owner Has full admin privileges and ownership Previous Owner Becomes an admin (not owner) Leave Group Previous owner can now leave the group Event Other members receive onOwnershipChanged event
Error Response (CometChatException ): Parameter Type Description errorCode String Unique error code. Example: "ERR_GROUP_NOT_JOINED" errorDescription String Human-readable error message. Example: "The user is not a member of the group"
Sample Payload - Transfer Ownership Error (Not Owner)
Request Parameters: Parameter Type Description UID String Unique identifier of the new owner. Example: "cometchat-uid-2" GUID String Unique identifier of the group. Example: "cometchat-guid-1"
Error Response: Parameter Type Description errorCode String Unique error code. Example: "ERR_PERMISSION_DENIED" errorDescription String Human-readable error message. Example: "User is not the owner of the group"
Only the current owner can transfer ownership.
Sample Payload - Transfer Ownership Error (To Self)
Request Parameters: Parameter Type Description UID String Unique identifier (same as current owner). Example: "cometchat-uid-1" GUID String Unique identifier of the group. Example: "cometchat-guid-1"
Error Response: Parameter Type Description errorCode String Unique error code. Example: "ERR_CANNOT_TRANSFER_TO_SELF" errorDescription String Human-readable error message. Example: "Cannot transfer ownership to yourself"
Real-Time Ownership Changed Events
In other words, as a member of a group, how do I know when ownership is transferred?
In order to receive real-time events whenever group ownership changes, you will need to implement the onOwnershipChanged() method of the CometChatGroupDelegate.
class ViewController : UIViewController , CometChatGroupDelegate {
override func viewDidLoad () {
super . viewDidLoad ()
CometChat. groupdelegate = self
}
func onOwnershipChanged ( group : Group, newOwner : GroupMember) {
print ( "Ownership changed in group: \( group. name ?? "" ) " )
print ( "New owner: \( newOwner. name ?? "" ) " )
}
}
onOwnershipChanged Parameters
Parameter Type Description group Group Group where ownership changed newOwner GroupMember The new owner of the group
Sample Payload - onOwnershipChanged Event
Event Trigger: Received via CometChatGroupDelegate.onOwnershipChanged(group:newOwner:)group (Group Object): Parameter Type Description guid String Unique group identifier. Example: "cometchat-guid-1" name String? Group display name. Example: "My Group" owner String? UID of the new owner. Example: "cometchat-uid-2" membersCount Int Total number of members. Example: 5
newOwner (GroupMember Object): Parameter Type Description uid String? Unique identifier of the new owner. Example: "cometchat-uid-2" name String? Display name of the new owner. Example: "George Alan" avatar String? URL to the new owner’s avatar. Example: "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" scope MemberScope New owner’s scope. Example: .admin
Missed Ownership Changed Events
In other words, as a member of a group, how do I know when ownership is transferred when my app is not running?
When you retrieve the list of previous messages if ownership has been transferred for any group that the logged-in user is a member of, the list of messages will contain an Action message.
Action Message Properties (for missed events)
Property Type Description action String "ownershipChanged"actionOn User New owner actionBy User Previous owner who transferred actionFor Group Group where change occurred
Common Error Codes
Error Code Description Resolution ERR_GROUP_NOT_FOUND Group with specified GUID does not exist Verify the GUID is correct ERR_UID_NOT_FOUND Target user does not exist Verify the user UID ERR_GROUP_NOT_JOINED User is not a member of the group Join the group first ERR_NOT_A_MEMBER Target user is not a member of the group Add user to group first ERR_PERMISSION_DENIED Logged-in user is not the group owner Only owner can transfer ownership ERR_CANNOT_TRANSFER_TO_SELF Cannot transfer ownership to yourself Specify a different user