User Management

With the user management API, you can create, delete users, update your profile information, add friends to your profile, remove friends from your profile.

Note that this user management will only work with CometChat v5.7 and custom coded integration.

To add User Management support in your application,

Include the MessageSDKBinding.iOS in the .cs file of your ViewController.

include-file

All the method calls will invoke their respective callback blocks as defined below.

1. Initialize the CometChat object

private CometChat _cometChat = new CometChat("API-KEY");

The API key can be obtained from CometChat administration panel. For this, perform following steps.

  1. login to your CometChat admin
  2. Goto settings tab, at the bottom you will find following lines
  3. APIkey

2. Create user
Create a user with given userName and password. (Ensure that userName and password cannot be nil or empty)

void CreateUser (string userName, string displayName, string password, string link, string avatar, Action<NSDictionary> success, Action<NSError> failure);

Parameters:
(i). userName: Name of the user
(ii). displayName: Explicit name of the user to be displayed
(iii). password: Password to login to CometChat
(iv). link: Link associated with the user
(v). avatar: Avatar of the user

Return type: void
Callback Blocks:
(i) Action<NSDictionary> success
Invocation: After the user is created successfully.
Response: NSDictionary containing the status and the message
Eg:

{
   "status" = "1000";
   "message" = "User created successfully!";
   "data" = {
             "userid" = 28;
            }
}

(ii) Action<NSError> failure
Invocation: If there’s an error while creating user.
Response: NSError containing error code and message
Eg:

Error Domain=USER MANAGEMENT ERROR Code=1001 
      "Username already exists." UserInfo=0x7feeb26e0d90 
      {NSLocalizedDescription=Username already exists.} 

Usage:

private void OnSuccess(NSDictionary dict) {
	Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
	Console.WriteLine("OnFailure : " + err);
}

this._cometChat.CreateUser("Ruby Doe",
                             "ruby",
                             "123",
                             "",
                             "",
                             (dict) => { this.OnSuccess(dict); },
                             (err) => { this.OnFailure(err);});

3. Update user details
Update any of the following user details. originalPassword is required to change any detail.(Ensure that originalPassword cannot be nil or empty)

void UpdateDisplayName (string displayName, string newPassword, string link, string avatarPath, string userID, string originalPassword, bool flag, Action<NSDictionary> success, Action<NSError> failure);

Parameters:
(i). displayName: Display name of the user to be updated
(ii). newPassword: Password of the user to be updated
(iii). link: Link of the user to be updated
(iv). avatar: Avatar of the user to be updated
(v). userID: userID of the user whose details have to be updated. If userID is NULL then details of the logged-in user will be updated.
(vi). originalPassword: Current password of the user whose details have to be updated.
If userID is NULL then pass the logged-in user’s current password
(vii). flag: If flag = YES, NULL OR empty values i.e @”” passed will be updated to default/empty values
If flag = NO, NULL OR empty values i.e @”” passed will not be updated

Return type: void
Callback Blocks:
(i) Action<NSDictionary> success
Invocation: After the user’s details have been updated successfully.
Response: NSDictionary containing the status and the message
Eg:

{
     "status" = "1000";
     "message" = "Details updated successfully!";
}

(ii) Action<NSError> failure
Invocation: If there’s an error while updating user’s details.
Response: NSError containing error code and message
Eg:

 Error Domain=USER MANAGEMENT ERROR Code=1003 "User not found." 
           UserInfo=0x7feeb26e0d90 {NSLocalizedDescription=User not found.}

Usage:
a) If you want to set only the display name

private void OnSuccess(NSDictionary dict) {
	Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
	Console.WriteLine("OnFailure : " + err);
}

this._cometChat.UpdateDisplayName("user",
                             NULL,
                             NULL,
                             NULL,
                             NULL,
                             "123",
                             false,
                             (dict) => { this.OnSuccess(dict); },
                             (err) => { this.OnFailure(err);});

b) If you want to set the display name and reset other values to default

private void OnSuccess(NSDictionary dict) {
	Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
	Console.WriteLine("OnFailure : " + err);
}

this._cometChat.UpdateDisplayName(NULL,
                             NULL,
                             NULL,
                             NULL,
                             NULL,
                             "123",
                             true,
                             (dict) => { this.OnSuccess(dict); },
                             (err) => { this.OnFailure(err);});

4. Remove user
Remove/Delete a user from CometChat by providing userID. (Ensure that userID cannot be nil or empty)

void RemoveUserByID (string userID, Action<NSDictionary> success, Action<NSError> failure);

Parameters:
(i). userID: userID of the user to be removed/deleted
Return type: void
Callback Blocks:
(i) Action<NSDictionary> success
Invocation: After the user is removed successfully.
Response: NSDictionary containing the status and the message
Eg:

{
       "status" = "1000";
       "message" = "User deleted successfully!";
       "data" = {
                 "userid" = "28"
                }
}

(ii) Action<NSError> failure
Invocation: If there’s an error while removing user.
Response: NSError containing error code and message
Eg:

Error Domain=USER MANAGEMENT ERROR Code=1003 "User not found." 
          UserInfo=0x7feeb26e0d90 {NSLocalizedDescription=User not found.}

Usage:

private void OnSuccess(NSDictionary dict) {
	Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
	Console.WriteLine("OnFailure : " + err);
}

this._cometChat.RemoveUserByID("28",
                             (dict) => { this.OnSuccess(dict); },
                             (err) => { this.OnFailure(err);});

5. Add friends
Add friends to logged-in user by providing array of user’s ID. (Ensure that userIDArray cannot be nil or empty)

void AddFriends (string[] userIDArray, Action<NSDictionary> success, Action<NSError> failure);

Parameters:
(i). userIDArray : An array containing user’s ID who have to be added as friends

Return type: void
Callback Blocks:
(i) Action<NSDictionary> success
Invocation: After the user/users have been added successfully.
Response: NSDictionary containing the status and the message
Eg:

{
      "status" = "1000";
      "message" = "Friends added successfully!";
      "data" = {
                "userid":"4"
               }
}

Here, data will contain the userID of only those users who have been added successfully

(ii) Action<NSError> failure
Invocation: If there’s error while adding all user’s as friends
Response: NSError containing error code and message
Eg:

Error Domain=USER MANAGEMENT ERROR Code=1006 "Failed to add friends!" 
                 UserInfo=0x7fc561e3c8b0 {NSLocalizedDescription=Failed to add friends!, 
                data={type = immutable dict, count = 1, entries =>0 : {contents = "userid"} = {contents = "2,3,45"} } }

Usage:

private void OnSuccess(NSDictionary dict) {
	Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
	Console.WriteLine("OnFailure : " + err);
}

this._cometChat.AddFriends(["2","3","45"],
                             (dict) => { this.OnSuccess(dict); },
                             (err) => { this.OnFailure(err);});

6. Remove friends
Remove friends of logged-in user by providing array of user’s ID.(Ensure that userIDArray should not be nil or empty)

void RemoveFriends (string[] userIDArray, Action<NSDictionary> success, Action<NSError> failure);

Parameters:
(i). userIDArray: An array containing user’s ID who have to be removed as friends

Return type: void
Callback Blocks:
(i) Action<NSDictionary> success
Invocation: After the user/users have been removed successfully.
Response: NSDictionary containing the status and the message
Eg:

{
       "status" = "1000";
       "message" = "Friends removed successfully!";
       "data" = {
                 "userid":"4"
               }
}

Here, data will contain the userid of only those users who have been removed successfully

(ii) Action<NSError> failure
Invocation: If there’s error while removing all user’s as friends
Response: NSError containing error code and message
Eg:

Error Domain=USER MANAGEMENT ERROR Code=1006 "Failed to remove friends!" 
         UserInfo=0x7fc561e3c8b0 {NSLocalizedDescription=Failed to remove  friends!, 
         data={type = immutable dict, count = 1, entries =>0 : {contents = "userid"} = {contents = "2,3,45"} } }

Usage:

private void OnSuccess(NSDictionary dict) {
	Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
	Console.WriteLine("OnFailure : " + err);
}

this._cometChat.RemoveFriends(["2","3","45"],
                             (dict) => { this.OnSuccess(dict); },
                             (err) => { this.OnFailure(err);});

Please refer ‎error codes for failure responses.

Not finding what you need?

The CometChat team is here to help!