To add Login support in your application,
Include the class <MessageSDKFramework/CometChat.h> in the .h file of your class.
All the method calls will invoke their respective callback blocks as defined below.
1. Initialize CometChat object
CometChat *cometchat = [[CometChat alloc] initWithAPIKey:@”Your API Key”];
2. Login to CometChat
Login to CometChat Cloud by any of the 2 available methods.
a) You can login to CometChat using the userID. All data related to the previous user will be cleared.
- (void)loginWithUserID:(NSString *)userID
success:(void(^)(NSDictionary *response))response
failure:(void(^)(NSError *error))failure;
Return type: void
Callback Blocks:
(i). (void(^)(NSDictionary *response))response
Invocation: After the login operation is completed successfully
Response: NSDictionary containing login success message
Eg:
{
"message"="Login Successful";
}
(ii). (void(^)(NSError *error))failure
Invocation: If there’s an error while performing the login operation
Response: NSError containing error code and message
Usage:
[cometchat loginWithUserID:@”5”
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
b) You can also login to CometChat using the username and password. All data related to the previous user will be cleared.
- (void)loginWithUsername:(NSString *)username
password:(NSString *)password
success:(void(^)(NSDictionary *response))response
failure:(void(^)(NSError *error))failure;
Return type: void
Callback Blocks:
(i). (void(^)(NSDictionary *response))response
Invocation: After the login operation is completed successfully
Response: NSDictionary containing login success message
Eg:
{
"message"="Login Successful";
}
(ii). (void(^)(NSError *error))failure
Invocation: If there’s an error while performing the login operation
Response: NSError containing error code and message
Usage:
[cometchat loginWithUsername:@”Tony Stark”
password:@”password”
success:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
Once you have logged-in to CometChat you can use all other features and functionalities available in CometChat SDK.
3. Logout
Calling this function will end the current session and clears user data
- (void)logoutWithSuccess:(void(^)(NSDictionary *response))response
failure:(void(^)(NSError *error))failure;
Return type: void
Callback Blocks:
(i). (void(^)(NSDictionary *response))response
Invocation: After the logout operation is completed
Response: NSDictionary containing logout message.
(ii). (void(^)(NSError *error))failure
Invocation: If there’s an error while performing the logout operation
Response: NSError containing error code and message
Usage:
[cometchat logoutWithSuccess:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
Not finding what you need?
The CometChat team is here to help!