To add Login support in your application,
Include the class <MessageSDKFramework/MessageSDK.h> in the .h file of your class.
All the method calls will invoke their respective callback blocks as defined below.
1. Initialize MessageSDK object
MessageSDK *msgSDK = [[MessageSDK alloc] init];
2. Login to CometChat
Login to CometChat by any of the 2 available methods. Please 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 *)url
userid:(NSString *)username
observer:(UIViewController *)currentView
success:(void (^)(NSDictionary *))success
userinfo:(void (^)(NSDictionary *))userinfo
chatroominfo:(void (^) (NSDictionary *))chatroominfo
onMessageReceive:(void (^) (NSDictionary *))onMessageReceive
failure:(void (^)(NSError *))failure;
Return type: void
Usage:
[msgSDK loginWithURL:@”http://yoursite.com/cometchat” userid:@”5” observer:self
success:^(NSDictionary *response) {
} userinfo:^(NSDictionary *response) {
} chatroominfo:^(NSDictionary *response) {
} onMessageReceive:^(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 *)url
username:(NSString *)username
password:(NSString *)password
observer:(UIViewController *)currentView
success:(void (^)(NSDictionary *))success
userinfo:(void (^)(NSDictionary *))userinfo
chatroominfo:(void (^) (NSDictionary *))chatroominfo
onMessageReceive:(void (^) (NSDictionary *))onMessageReceive
failure:(void (^)(NSError *))failure;
Return type: void
Usage:
[msgSDK loginWithURL:@”http://yoursite.com/cometchat” username:@”Tony Stark” password:@”password”
observer:self
success:^(NSDictionary *response) {
}userinfo:^(NSDictionary *response) {
}chatroominfo:^(NSDictionary *response) {
}onMessageReceive:^(NSDictionary *response) {
}failure:^(NSError *error) {
}];
With these callback, we have added following new functions which are as follows:
userinfo callback : This function will provide developer with firebase channel for One-on-One Messages and Announcement messages values. The response JSON will be in following format:
{
"push_an_channel" = "ANN_08dd0d0d94b475e429320f76a48b06fei";
"push_channel" = "C_339dbd60e377fbef6307320f26b93355i";
}
chatroominfo callback : This function will provide developer with firebase channel and other information for a chatroom when the user joins/leave the chatroom. The response JSON will be in following format:
i] join chatroom response :
{
action = join;
id = 28;
"push_channel" = "C_555dbd60e377fbef9657320f26b45855i";
}
ii] leave chatroom response :
{
action = leave;
id = 28;
}
onMessageReceive callback : This function will be triggered whenever the message is received in One-On-One and Chatroom. The response JSON will be in following format:
i] One-On-One message :
{
"message_id" = 47;
"user_id" = 98;
}
ii] Chatroom message :
{
"chatroom_id" = 1;
"message_id" = 1061;
"user_id" = 55;
}
3. Launch chat with window
In order to open the chat with specific user, use chatWith method. The syntax for this function is as follows:
- (void)chatWith:(NSString *)userid
setBackButton:(BOOL)flag
observer:(UIViewController *)currentView;
Note : The user name will be displayed at the status bar if the user is online.
Return type: void
Usage:
[msgSDK chatWith:@"4" setBackButton:YES observer:self];
4. Logout
Calling this function will end the current session and clears user data
- (void)logoutWithSuccess:(void (^)(NSDictionary *))success
failure:(void (^)(NSError *))failure;
Usage:
[msgSDK logoutWithSuccess:^(NSDictionary *response) {
} failure:^(NSError *error) {
}];
Not finding what you need?
The CometChat team is here to help!