> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-ios-ui-kit-sdk-fixes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Transfer Group Ownership

> Guide to transferring group ownership using the CometChat iOS SDK transferGroupOwnership method (owner only).

<Info>
  **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](/sdk/ios/leave-group) · [Change Member Scope](/sdk/ios/group-change-member-scope) · [Groups Overview](/sdk/ios/groups-overview)
</Info>

## 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.

<Warning>
  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.
</Warning>

### Method Parameters

| Parameter | Type   | Description              |
| --------- | ------ | ------------------------ |
| UID       | String | The UID of the new owner |
| GUID      | String | The GUID of the group    |

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    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)")
    })
    ```
  </Tab>
</Tabs>

<Accordion title="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](#common-error-codes)):**

  | 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"` |
</Accordion>

<Accordion title="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"` |

  <Warning>Only the current owner can transfer ownership.</Warning>
</Accordion>

<Accordion title="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"` |
</Accordion>

***

## 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`.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    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 ?? "")")
        }
    }
    ```
  </Tab>
</Tabs>

### onOwnershipChanged Parameters

| Parameter | Type                                                                         | Description                   |
| --------- | ---------------------------------------------------------------------------- | ----------------------------- |
| group     | [Group](/sdk/ios/retrieve-groups#group-properties)                           | Group where ownership changed |
| newOwner  | [GroupMember](/sdk/ios/retrieve-group-members#groupmember-object-properties) | The new owner of the group    |

<Accordion title="Sample Payload - onOwnershipChanged Event">
  **Event Trigger:** Received via `CometChatGroupDelegate.onOwnershipChanged(group:newOwner:)`

  **group ([Group](/sdk/ios/retrieve-groups#group-properties) 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](/sdk/ios/retrieve-group-members#groupmember-object-properties) 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](/sdk/ios/retrieve-group-members#memberscope-enum) | New owner's scope. Example: `.admin`                                                                            |
</Accordion>

## 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](/sdk/ios/users-overview#user-properties)    | New owner                      |
| actionBy  | [User](/sdk/ios/users-overview#user-properties)    | Previous owner who transferred |
| actionFor | [Group](/sdk/ios/retrieve-groups#group-properties) | 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          |
