Login/Logout

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] init];

2. Login to CometChat
Login to CometChat on your website by any of the 3 available methods. Also make sure that your chatlogin() function in integration.php should be configured to handle any of the login mechanisms you opt for.

a) You can login to CometChat using the userID. All data related to the previous user will be cleared.

- (void)loginWithURL:(NSString *)siteURL
              userID:(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 loginWithURL:@”http://yoursite.com/cometchat”    
                 userID:@”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)loginWithURL:(NSString *)siteURL
            username:(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 loginWithURL:@”http://yoursite.com/cometchat”    
               username:@”Tony Stark”
               password:@”password” 
               success:^(NSDictionary *response) {
             } failure:^(NSError *error) {
    	}];

(c) Login as a guest user in CometChat. If the user has not exclusively called logout, the session will be retained. If there is no existing session, a new guest user will be created.

- (void)guestLoginWithURL:(NSString *)siteURL
                     name:(NSString *)name
                  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 guestLoginWithURL:@”http://yoursite.com/cometchat”    
                        name:@”Samantha”
                     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!