QBChatDialog *dialogInfo = [QBChatDialog create:QBChatDialogTypePrivate];dialogInfo.occupantIDs = @[@34]; // an ID of opponent[QBRequest createDialog:dialogInfo successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nonnull dialog) {} errorBlock:^(QBResponse * _Nonnull response) {}];
To create group dialog for a predefined number of occupants, you need to set the dialog type field to group and IDs of opponents you want to create a chat with.
Copy
let dialogInfo = QBChatDialog.create(.group)dialogInfo.name = "New group dialog"dialogInfo.occupantIDs = [34, 45, 55]// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = "...";let dialog = try await QBRequest.createDialog(dialogInfo)try await dialog.join()
Copy
let dialogInfo = QBChatDialog.create(.group)dialogInfo.name = "New group dialog"dialogInfo.occupantIDs = [34, 45, 55]// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = "...";let dialog = try await QBRequest.createDialog(dialogInfo)try await dialog.join()
Copy
let dialogInfo = QBChatDialog.create(.group)dialogInfo.name = "New group dialog"dialogInfo.occupantIDs = [34, 45, 55]// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = "...";QBRequest.createDialog(dialogInfo, successBlock: { (response, dialog) in dialog.join(completionBlock: { (error) in })}, errorBlock: { (response) in})
Copy
QBChatDialog *dialogInfo = [QBChatDialog create:QBChatDialogTypeGroup];dialogInfo.name = @"Group dialog name";dialogInfo.occupantIDs = @[@34, @45, @55];// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = @"...";[QBRequest createDialog:dialogInfo successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nonnull dialog) { [dialog joinWithCompletionBlock:^(NSError * _Nullable error) { }];} errorBlock:^(QBResponse * _Nonnull response) {}];
It’s possible to create a public dialog, so any user from your application can be joined to it. There is no list of occupants. This dialog is open for everybody. You need to set the dialog type field to public.
Copy
let dialogInfo = QBChatDialog.create(.publicGroup)dialogInfo.name = "Public dialog name"dialogInfo.dialogDescription = "Public dialog description"// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = "...";let dialog = try await QBRequest.createDialog(dialogInfo)try await dialog.join()
Copy
let dialogInfo = QBChatDialog.create(.publicGroup)dialogInfo.name = "Public dialog name"dialogInfo.dialogDescription = "Public dialog description"// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = "...";let dialog = try await QBRequest.createDialog(dialogInfo)try await dialog.join()
Copy
let dialogInfo = QBChatDialog.create(.publicGroup)dialogInfo.name = "Public dialog name"dialogInfo.dialogDescription = "Public dialog description"// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = "...";QBRequest.createDialog(dialogInfo, successBlock: { (response, dialog) in dialog.join(completionBlock: { (error) in })}, errorBlock: { (response) in})
Copy
QBChatDialog *dialogInfo = [QBChatDialog create:QBChatDialogTypePublicGroup];dialogInfo.name = @"Public dialog name";dialogInfo.dialogDescription = @"Public dialog description";// Photo can be a link to a file in Content module, Custom Objects module or just a web link.// dialogInfo.photo = @"...";[QBRequest createDialog:dialogInfo successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nonnull dialog) { [dialog joinWithCompletionBlock:^(NSError * _Nullable error) { }];} errorBlock:^(QBResponse * _Nonnull response) {}]
Any dialog can be extended with additional parameters whether it is a private, group, or public. These parameters can be used to store additional data. Also, these parameters can be used in dialogs retrieval request.
To start using additional parameters, create an additional schema of your parameters. This is a Custom Objects class. Just create an empty class with all fields that you need. These fields will be additional parameters in your dialog. See this section to learn how to create a schema using Custom Objects.
Then, specify the parameters defined in the schema in a new dialog.
Copy
let dialogInfo = QBChatDialog.create(.group)dialogInfo.name = "Movies"dialogInfo.occupantIDs = [34, 45, 55]let customParameters = ["class_name": "Movie", "name": "Star Wars", "rating": 9.1, "documentary": false, "genre": "fantasy", "descriptions": "Star Wars is an American epic space opera."]dialogInfo.data = customParameterslet dialog = try await QBRequest.createDialog(dialogInfo)try await dialog.join()
Copy
let dialogInfo = QBChatDialog.create(.group)dialogInfo.name = "Movies"dialogInfo.occupantIDs = [34, 45, 55]let customParameters = ["class_name": "Movie", "name": "Star Wars", "rating": 9.1, "documentary": false, "genre": "fantasy", "descriptions": "Star Wars is an American epic space opera."]dialogInfo.data = customParameterslet dialog = try await QBRequest.createDialog(dialogInfo)try await dialog.join()
Copy
let dialogInfo = QBChatDialog.create(.group)dialogInfo.name = "Movies"dialogInfo.occupantIDs = [34, 45, 55]let customParameters = ["class_name": "Movie", "name": "Star Wars", "rating": 9.1, "documentary": false, "genre": "fantasy", "descriptions": "Star Wars is an American epic space opera."]dialogInfo.data = customParametersQBRequest.createDialog(dialogInfo, successBlock: { (response, dialog) in //Block with response and user instances if the request is succeeded. dialog.join(completionBlock: { (error) in })}, errorBlock: { (response) in //Block with response instance if the request is failed.})
Copy
QBChatDialog *dialogInfo = [QBChatDialog create:QBChatDialogTypeGroup];dialogInfo.name = @"Movies";dialogInfo.occupantIDs = @[@34, @45, @55];NSDictionary<NSString *, id> *customParameters = @{@"class_name": @"Movie", @"name": @"Star Wars", @"rating": @(9.1), @"documentary": @(false), @"genre": @"fantasy", @"descriptions": @"Star Wars is an American epic space opera franchise consisting of a film series created by George Lucas."};dialogInfo.data = customParameters;[QBRequest createDialog:dialogInfo successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nonnull dialog) { //Block with response and user instances if the request is succeeded. [dialog joinWithCompletionBlock:^(NSError * _Nullable error) { }];} errorBlock:^(QBResponse * _Nonnull response) { //Block with response instance if the request is failed.}];
Before you start chatting in a group or public dialog, you need to join it by calling the join() method. If you’ve successfully joined the dialog, you can send/receive messages in real-time. See this section to learn how to send/receive messages.
Copy
let groupDialog = ...try await groupDialog.join()
Copy
let groupDialog = ...try await groupDialog.join()
Copy
let groupDialog = ...groupDialog.join { (error) in}
Let’s see, how the join() method is used with regard to the dialog type.
Capabilities
Public
Group
Private
Join
✓
✓
✗
You can join a group dialog only if your user ID is present in the occupantIDs array, in the dialog model.
Your user ID is added to the occupantIDs array if you create a dialog or you are added to the dialog by the other user. See this section to learn how to add occupants to the group dialog.
To subscribe to the onJoinOccupant event, use the code snippet below. As a result, you will be notified by the SDK about the onJoinOccupant() event whenever the occupant has joined the current group dialog.
You can leave the group and public dialog by calling the leave() method. If the dialog is left, you can’t send/receive messages. To be able to receive/send messages, you need to join it.
Copy
let groupDialog = ...try await dialog.leave()
Copy
let groupDialog = ...try await dialog.leave()
Copy
let groupDialog = ...groupDialog.leave { (error) in}
Let’s see, how the leave() method is used with regard to the dialog type.
Capabilities
Public
Group
Private
Leave
✓
✓
✗
When a group dialog is left, your user ID is still present in the occupantIDs array, in the dialog model. As a result, the dialog will still be present in the list of dialogs and you will still have access to the chat history.
To remove yourself from the group dialog, use the update() method. See this section to learn how to remove occupants from the group dialog.
To subscribe to the onLeaveOccupant event, use the code snippet below. As a result, you will be notified by the SDK about the onLeaveOccupant() event whenever the occupant has left the current group dialog.
You can get a list of online users from the chatDialog dialog. Call the requestOnlineUsers() method to get the list of online users who are joined to the dialog. As a result, a completion block with an array of user IDs or failure error is called.
It’s common to request all your dialogs on every app login. The request below will return private, group, and public dialogs that have been updated during the last month, sorted by the last_message_date_sent in descending order, and limited to 10 dialogs per page.
Copy
//get dialogs that have been updated during the last month and sort by the date of the last message in descending orderlet monthAgoDate = Calendar.current.date( byAdding: .month, value: -1, to: Date())let timeInterval = monthAgoDate!.timeIntervalSince1970let paramSort = "sort_desc"let sortValue = "last_message_date_sent"let paramFilter = "updated_at[gte]"let filterValue = "\(timeInterval)"var extendedRequest: [String: String] = [:]extendedRequest[paramSort] = sortValueextendedRequest[paramFilter] = filterValuelet responsePage = QBResponsePage(limit: 10)QBRequest.dialogs(for: responsePage, extendedRequest: extendedRequest, successBlock: { response, dialogs, dialogsUsersIDs, page in}, errorBlock: { response in})
Copy
//get dialogs that have been updated during the last month and sort by the date of the last message in descending orderlet monthAgoDate = Calendar.current.date( byAdding: .month, value: -1, to: Date())let timeInterval = monthAgoDate!.timeIntervalSince1970let paramSort = "sort_desc"let sortValue = "last_message_date_sent"let paramFilter = "updated_at[gte]"let filterValue = "\(timeInterval)"var extendedRequest: [String: String] = [:]extendedRequest[paramSort] = sortValueextendedRequest[paramFilter] = filterValuelet responsePage = QBResponsePage(limit: 10)QBRequest.dialogs(for: responsePage, extendedRequest: extendedRequest, successBlock: { response, dialogs, dialogsUsersIDs, page in}, errorBlock: { response in})
Copy
//get dialogs that have been updated during the last month and sort by the date of the last message in descending orderNSDate *monthAgoDate = [NSCalendar.currentCalendar dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:[NSDate date] options:0];NSTimeInterval timeInterval = [monthAgoDate timeIntervalSince1970];NSString *paramSort = @"sort_desc";NSString *sortValue = @"last_message_date_sent";NSString *paramFilter = @"updated_at[gte]";NSString *filterValue = @(timeInterval).stringValue;NSMutableDictionary *extendedRequest = [NSMutableDictionary dictionary];extendedRequest[paramSort] = [NSString stringWithFormat:@"%@", sortValue];extendedRequest[paramFilter] = [NSString stringWithFormat:@"%@", filterValue];QBResponsePage *responsePage = [QBResponsePage responsePageWithLimit:10];[QBRequest dialogsForPage:responsePage extendedRequest:extendedRequest successBlock:^(QBResponse *response, NSArray *dialogs, NSSet *dialogsUsersIDs, QBResponsePage *page) {} errorBlock:^(QBResponse *response) {}];
Argument
Required
Description
responsePage
no
If you want to get a paginated list of users from the server, you can set the following fields of the responsePage:- skip allows to skip N records in search results. Default (if not specified): 0.- limit allows limit search results to N records. Default value: 100.
extendedRequest
yes
A dictionary that stores keys and values of the String type. The keys are formed as parameters of the List Dialogs request.
If you want to retrieve only dialogs updated after some specific date time and order the search results, you can apply operators. This is useful if you cache dialogs somehow and do not want to obtain the whole list of dialogs on every app start. Thus, you can apply search and sort operators to list dialogs on the page so that it is easier to view specific dialogs. The operators are set as key-value parameters in the extendedRequest dictionary.
You can use search operators to get more specific search results. The request below will return 10 dialogs that were updated over the last month.
Copy
//get dialogs that have been updated over the last monthlet monthAgoDate = Calendar.current.date( byAdding: .month, value: -1, to: Date())let timeInterval = monthAgoDate!.timeIntervalSince1970let paramFilter = "updated_at[gte]"let filterValue = "\(timeInterval)"var extendedRequest: [String: String] = [:]extendedRequest[paramFilter] = filterValuelet responsePage = QBResponsePage(limit: 10)QBRequest.dialogs(for: responsePage, extendedRequest: extendedRequest, successBlock: { response, dialogs, dialogsUsersIDs, page in}, errorBlock: { response in})
Copy
//get dialogs that have been updated over the last monthlet monthAgoDate = Calendar.current.date( byAdding: .month, value: -1, to: Date())let timeInterval = monthAgoDate!.timeIntervalSince1970let paramFilter = "updated_at[gte]"let filterValue = "\(timeInterval)"var extendedRequest: [String: String] = [:]extendedRequest[paramFilter] = filterValuelet responsePage = QBResponsePage(limit: 10)QBRequest.dialogs(for: responsePage, extendedRequest: extendedRequest, successBlock: { response, dialogs, dialogsUsersIDs, page in}, errorBlock: { response in})
Copy
//get dialogs that have been updated during the last month and sort by the date of the last message in descending orderNSDate *monthAgoDate = [NSCalendar.currentCalendar dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:[NSDate date] options:0];NSTimeInterval timeInterval = [monthAgoDate timeIntervalSince1970];NSString *paramFilter = @"updated_at[gte]";NSString *filterValue = @(timeInterval).stringValue;NSMutableDictionary *extendedRequest = [NSMutableDictionary dictionary];extendedRequest[paramFilter] = [NSString stringWithFormat:@"%@", filterValue];QBResponsePage *responsePage = [QBResponsePage responsePageWithLimit:10];[QBRequest dialogsForPage:responsePage extendedRequest:extendedRequest successBlock:^(QBResponse *response, NSArray *dialogs, NSSet *dialogsUsersIDs, QBResponsePage *page) {} errorBlock:^(QBResponse *response) {}];
Here are the search operators that you can use to search for the exact data that you need.
You can use sort operators to order the search results. The request below will return dialogs sorted in descending order by the last_message_date_sent field.
You can update the information for a private, group, and public dialog.
Copy
chatDialog.name = "New dialog name"chatDialog.dialogDescription = "New dialog description"chatDialog.photo = "https://new_photo_url" // or it can be an ID to some file in Storage moduleQBRequest.update(chatDialog, successBlock: { (response, updatedDialog) in}, errorBlock: { (response) in})
Copy
chatDialog.name = "New dialog name"chatDialog.dialogDescription = "New dialog description"chatDialog.photo = "https://new_photo_url" // or it can be an ID to some file in Storage moduleQBRequest.update(chatDialog, successBlock: { (response, updatedDialog) in}, errorBlock: { (response) in})
Copy
dialog.name = @"New dialog name";dialog.dialogDescription = @"New dialog description";dialog.photo = @"https://new_photo_url"; // or it can be an ID to some file in Storage module[QBRequest updateDialog:dialog successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nonnull updatedDialog) {} errorBlock:^(QBResponse * _Nonnull response) {}];
Let’s see what capabilities a particular user role has with regard to the dialog type.
You can get a number of dialogs using the countOfDialogs(withExtendedRequest:) method. The request below will return a count of dialogs updated over the last month.
Copy
//get dialogs that have been updated during the last month and sort by the date of the last message in descending orderlet monthAgoDate = Calendar.current.date( byAdding: .month, value: -1, to: Date())let timeInterval = monthAgoDate!.timeIntervalSince1970let paramFilter = "updated_at[gte]"let filterValue = "\(timeInterval)"var extendedRequest: [String: String] = [:]extendedRequest[paramFilter] = filterValueQBRequest.countOfDialogs(withExtendedRequest: extendedRequest, successBlock: { response, count in}, errorBlock: { response in})
Copy
//get dialogs that have been updated during the last month and sort by the date of the last message in descending orderlet monthAgoDate = Calendar.current.date( byAdding: .month, value: -1, to: Date())let timeInterval = monthAgoDate!.timeIntervalSince1970let paramFilter = "updated_at[gte]"let filterValue = "\(timeInterval)"var extendedRequest: [String: String] = [:]extendedRequest[paramFilter] = filterValueQBRequest.countOfDialogs(withExtendedRequest: extendedRequest, successBlock: { response, count in}, errorBlock: { response in})
Copy
//get dialogs that have been updated during the last month and sort by the date of the last message in descending orderNSDate *monthAgoDate = [NSCalendar.currentCalendar dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:[NSDate date] options:0];NSTimeInterval timeInterval = [monthAgoDate timeIntervalSince1970];NSString *paramFilter = @"updated_at[gte]";NSString *filterValue = @(timeInterval).stringValue;NSMutableDictionary *extendedRequest = [NSMutableDictionary dictionary];extendedRequest[paramFilter] = [NSString stringWithFormat:@"%@", filterValue];[QBRequest countOfDialogsWithExtendedRequest:extendedRequest successBlock:^(QBResponse * _Nonnull response, NSUInteger count) {} errorBlock:^(QBResponse *response) {}];
Argument
Required
Description
extendedRequest
yes
A dictionary that stores keys and values of the String type. The keys are formed as patameters of the List Dialogs request.
You can also retrieve the total number of unread messages using the totalUnreadMessageCountForDialogs() method . It returns the total count of unread messages for all dialogs of the user in totalUnreadCount. It also returns unread messages count for dialogs with IDs in dialogsDictionary if any dialogIDs are specified.
IDs of dialogs.- If dialogIDs are not specified, the total number of unread messages for all dialogs of the user will be returned.- If dialogIDs are specified, the number of unread messages for each specified dialog will be returned. Also, the total number of unread messages for all dialogs of the user will be returned.