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 above, along with custom coded integration.
To add User Management support in your application,
Include the class <CometChatSDK/CometChat.h> in the .h file of your class.
All the method calls will invoke their respective callback blocks as defined below.
1. Initialize the CometChat object
CometChat *cometChat = [[CometChat alloc] initWithAPIKey:@"API-KEY"];
The API key can be obtained from CometChat administration panel. For this, perform following steps.
2. Create user
Create a user with given userName and password. (Ensure that userName and password cannot be nil or empty)
- (void)createUser:(NSString *)userName
password:(NSString *)password
link:(NSString *)link
avatar:(NSString *)avatar
displayName:(NSString *)displayName
success:(void(^)(NSDictionary *response))success
failure:(void(^)(NSError *error))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) (void(^)(NSDictionary *response))response
Invocation: After the user is created successfully.
Response: NSDictionary containing the status and the message
Eg:
{
"status" = "1000";
"message" = "User created successfully!";
}
(ii) (void(^)(NSError *error))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:
[cometChat createUser:@"Ruby Doe"
displayName:@"ruby"
password:@"123"
link:@""
avatar:@""
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
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:(NSString *)displayName
password:(NSString *)newPassword
link:(NSString *)link
avatar:(NSString *)avatar
ofUser:(NSString *)userID
withPassword:(NSString *)originalPassword
setNULLValues:(BOOL)flag
success:(void(^)(NSDictionary *response))success
failure:(void(^)(NSError *error))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) (void(^)(NSDictionary *response))response
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) (void(^)(NSError *error))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
[cometChat updateDisplayName:@"user"
password:NULL
link:NULL
avatar:NULL
ofUser:NULL
withPassword:@"123"
setNULLValues:NO
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
b) If you want to set the display name and reset other values to default
[cometChat updateDisplayName:NULL
password:NULL
link:NULL
avatar:NULL
ofUser:NULL
withPassword:@"123"
setNULLValues:YES
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
4. Remove user
Remove/Delete a user from CometChat by providing userID. (Ensure that userID cannot be nil or empty)
- (void)removeUserByID:(NSString *)userID
success:(void(^)(NSDictionary *response))success
failure:(void(^)(NSError *error))failure;
Parameters:
(i). userID: userID of the user to be removed/deleted
Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
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) (void(^)(NSError *error))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:
[cometChat removeUserByID:@"28"
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
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:(NSArray *)userIDArray
success:(void(^)(NSDictionary *response))success
failure:(void(^)(NSError *error))failure;
Parameters:
(i). userIDArray : An array containing user’s ID who have to be added as friends
Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
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) (void(^)(NSError *error))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:
[cometChat addFriends:@[@"2",@"3",@"45"]
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
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:(NSArray *)userIDArray
success:(void(^)(NSDictionary *response))success
failure:(void(^)(NSError *error))failure;
Parameters:
(i). userIDArray: An array containing user’s ID who have to be removed as friends
Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
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) (void(^)(NSError *error))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:
[cometChat removeFriends:@[@"2",@"3",@"45"]
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
Please refer error codes for failure responses.
Not finding what you need?
The CometChat team is here to help!