SimpleSample-chat users-ios
Contents
|
Sources
Project homepage on GIT — https://github.com/QuickBlox/quickblox-ios-sdk/tree/master/sample-chat
Download ZIP - https://github.com/QuickBlox/quickblox-ios-sdk/archive/master.zip
Overview
This sample demonstrates how to work with QuickBlox Chat API.
It allows to organize chat in room & chat 1 on 1
It shows how to:
- Organize chat between 2 users
- Organize chat in room
Demo
Guide: Get Started with Chat API
Get QuickBlox account
http://admin.quickblox.com/register
Create application in Admin panel
http://admin.quickblox.com/apps/new
Also you can look through 5 min guide.
Connect QuickBlox to your application
To get the information on how to connect to the QuickBlox.framework, please, refer to the IOS-how-to-connect-Quickblox-framework page.
Add Chat to your application
Login to Chat
Note: In order to login to chat please read info about Chat login/password formation.
In order to use QuickBlox Chat APIs you must:
- Create session & Sign In to QuickBlox OR just create session with user
- Sign In to QuickBlox Chat
Please follow lines below:
Create session with User & Sign In to QuickBlox Chat
// Create session with user QBASessionCreationRequest *extendedAuthRequest = [QBASessionCreationRequest request]; extendedAuthRequest.userLogin = @"garry"; extendedAuthRequest.userPassword = @"garrySant88"; [QBAuth createSessionWithExtendedRequest:extendedAuthRequest delegate:self]; #pragma mark - #pragma mark QBActionStatusDelegate // QuickBlox queries delegate - (void)completedWithResult:(Result *)result{ // Create session result if(result.success && [result isKindOfClass:QBAAuthSessionCreationResult.class]){ // You have successfully created the session QBAAuthSessionCreationResult *res = (QBAAuthSessionCreationResult *)result; // Sign In to QuickBlox Chat QBUUser *currentUser = [QBUUser user]; currentUser.ID = res.session.userID; // your current user's ID currentUser.password = @"garrySant88"; // your current user's password // set Chat delegate [QBChat instance].delegate = self; // login to Chat [[QBChat instance] loginWithUser:currentUser]; } } #pragma mark - #pragma mark QBChatDelegate // Chat delegate -(void) chatDidLogin{ // You have successfully signed in to QuickBlox Chat }
Keep in mind that QuickBlox Chat is a standard XMPP chat and you need to send presence periodically to remain available. Just create timer that will send presence each 30 seconds:
[NSTimer scheduledTimerWithTimeInterval:30 target:[QBChat instance] selector:@selector(sendPresence) userInfo:nil repeats:YES];
1 to 1 Chat
1 to 1 Chat is very easy. Just send message to opponent & receive messages from him:
// send message QBChatMessage *message = [[QBChatMessage alloc] init]; message.recipientID = 546; // opponent's id message.text = @"Hi mate!"; [[QBChat instance] sendMessage:message]; #pragma mark - #pragma mark QBChatDelegate - (void)chatDidReceiveMessage:(QBChatMessage *)message{ NSLog(@"New message: %@ from user: %d", message.text, message.senderID); }
Chat in room
Create room
You can create 4 types of rooms:
- members-only vs. open:
- open - A room that non-banned users are allowed to enter without being on the member list
- members-only - A room that a user cannot enter without being on the member list
- persistent vs. temporary:
- persistent - A room that is not destroyed if the last user exits
- temporary - A room that is destroyed if the last occupant exits
Create open & persistent room
[[QBChat instance] createOrJoinRoomWithName:@"Real Madrid fans" membersOnly:NO persistent:YES]; #pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidEnter:(QBChatRoom *)room{ NSLog(@"Public room %@ was created", room.name); // You have to retain created room if this is temporary room. In other cases room will be destroyed and all occupants will be disconnected from room myRoom = [room retain]; }
Note: when room is released - you will leave it automatically.
Create members-only & temporary room
[[QBChat instance] createOrJoinRoomWithName:@"Real Madrid fans" membersOnly:YES persistent:NO]; #pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidEnter:(QBChatRoom *)room{ NSLog(@"Private room %@ was created", room.name); // You have to retain created room if this is temporary room. In other cases room will be destroyed and all occupants will be disconnected from room myRoom = [room retain]; // Add users to this room NSNumber *anny = [NSNumber numberWithInt:300]; NSNumber *jim = [NSNumber numberWithInt:357]; NSArray *users = [NSArray arrayWithObjects:anny, jim, nil]; [[QBChat instance] addUsers:users toRoom:room]; }
Manage members
Add users to only-members room
NSNumber *user = [NSNumber numberWithInt:298]; NSArray *users = [NSArray arrayWithObject:user]; [[QBChat instance] addUsers:users toRoom:myRoom];
Delete users from only-members room
NSNumber *user = [NSNumber numberWithInt:298]; NSArray *users = [NSArray arrayWithObject:user]; [[QBChat instance] deleteUsers:users fromRoom:myRoom]];
Retrieve users who can join only-members room
[[QBChat instance] requestRoomUsers:myRoom]; #pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidReceiveListOfUsers:(NSArray *)users room:(NSString *)roomName{ NSLog(@"chatRoomDidReceiveListOfUsers %@, %@",roomName, users); }
Retrieve online users
[[QBChat instance] requestRoomOnlineUsers:myRoom]; #pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidReceiveListOfOnlineUsers:(NSArray *)users room:(NSString *)roomName{ NSLog(@"chatRoomDidReceiveListOfOnlineUsers %@, %@",roomName, users); }
Handle room online users
#pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidChangeOnlineUsers:(NSArray *)onlineUsers room:(NSString *)roomName{ NSLog(@"chatRoomDidChangeOnlineUsers %@, %@",roomName, onlineUsers); }
Next, you can discover all rooms, try to join any room and leave as well:
Discover rooms, join/leave room
Discover all rooms
[[QBChat instance] requestAllRooms]; #pragma mark - #pragma mark QBChatDelegate - (void)chatDidReceiveListOfRooms:(NSArray *)_rooms{ NSLog(@"Did receive list of rooms:"); for (QBChatRoom* room in _rooms) { NSLog(@"%@",[room name]); } }
Join room
[[QBChat instance] joinRoom:realMadridFansRoom]; #pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidEnter:(QBChatRoom *)room{ NSLog(@"chatRoomDidEnter: %@", room); } - (void)chatRoomDidNotEnter:(NSString *)roomName error:(NSError *)error{ NSLog(@"chatRoomDidNotEnter: %@, error: %@", roomName, error); }
Leave room
[[QBChat instance] leaveRoom:realMadridFansRoom; #pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidLeave:(NSString *)roomName{ NSLog(@"chatRoomDidLeave: %@", roomName); }
Send & receive messages
In order to send & receive messages you must be joined to room.
Send message
[[QBChat instance] sendMessage:@"Hey! Did you see last Liverpool match?" toRoom:liverpoolFansRoom];
Receive messages
#pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidReceiveMessage:(QBChatMessage *)message fromRoom:(NSString *)roomName { NSLog(@"Did receive message: %@ from room:%@", message.text, roomName) }
Destroy room
Room owner can destroy room
[[QBChat instance] destroyRoom:testRoom]; #pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidDestroy:(NSString *)roomName{ NSLog(@"chatRoomDidDestroy: %@", roomName); }
Manage rooms in Admin panel
You can create room through Admin panel, Chat module
- Just Sign In on Admin panel, go to Chat module page
- Enter your password from Admin panel again.
- Press New Room button - New room form will appear
- Enter room's name and press Create button
- Your room will be appear in List of rooms. Now you can discover it & join
- If you want to set some room's options - click on it - page with room's details will be oppened
Manage Chat History
1 to 1 Chat history
There are 3 ways how to organize 1 to 1 chat history:
- Store history on client side (e.g. using DataBase or similar features)
- Use Custom Objects - create separate table, store history to it and retrieve when need
- Use rooms for 1 to 1 chat
Room history
After joined a room you will receive the history as regular messages.
#pragma mark - #pragma mark QBChatDelegate - (void)chatRoomDidReceiveMessage:(QBChatMessage *)message fromRoom:(NSString *)roomName{ // message.delayed = 1 for room history messages }
By default, maximum number of history messages returned by room is 50. There is no way to change this value now.
Conclusion
Now you can play around QuickBlox Chat API, integrate Chat to your own applications and more & more with QuickBlox!
Comments
Feel free to comment on this page using the form below.
blog comments powered by Disqus



