【发布时间】:2015-12-23 09:29:11
【问题描述】:
寻求在我们的 iOS 应用中实现 QuickBlox 的帮助。我们似乎无法解决一个问题,即在发送消息后,QuickBlox 会返回之前在计时器滴答事件调用块时发送的所有消息。任何帮助将不胜感激!
QBResponsePage *page = [QBResponsePage responsePageWithLimit:10 skip:0];
[QBRequest dialogsForPage:page extendedRequest:nil successBlock:^(QBResponse *response, NSArray *dialogObjects, NSSet *dialogsUsersIDs, QBResponsePage *page)
{
QBChatDialog *dialog=[dialogObjects objectAtIndex:0];
[QBRequest messagesWithDialogID:dialog.ID extendedRequest:nil forPage:page successBlock:^(QBResponse *response, NSArray *messages, QBResponsePage *responsePage)
{
[self.items removeAllObjects];
[self.items addObjectsFromArray:messages];
[self finishSendingMessageAnimated:YES];
} errorBlock:^(QBResponse *response) {
NSLog(@"error: %@", response.error);
}];
} errorBlock:^(QBResponse *response) {
}];
以下是我们采取的步骤: 1) 将所有 QuickBlox 类导入到我们的项目中。
2) 在 My App Delegate Class 中设置 QuickBlox 的所有凭据
[QBApplication sharedApplication].applicationId = (removed);
[QBConnection registerServiceKey:@“(removed)”];
[QBConnection registerServiceSecret:@“(removed)”];
[QBSettings setAccountKey:@“(removed)”];
3) 然后创建了导入的QMChatViewController的子类
4) 导入的 NMPaginatorDelegate 和 Cretae UsersPaginator 对象,(给我用户列表)
5)点击特定用户调用我们创建的子类,并在其 viewDidLoad 上写下以下代码:
QBUUser *currentUser = [QBUUser user];
currentUser.ID = LoginUser.ID;
currentUser.password = LoginUser.login;
[[QBChat instance] addDelegate:(id)self];
[[QBChat instance] loginWithUser:currentUser];
loginWithUser 调用后
BOOL chlLogion =[[QBChat instance]isLoggedIn];
NSLog(@"Loggin IN Status :- %hhd”,chlLogion);
日志打印:- 2015-09-22 16:04:31.911 AppName[2263:96572] [ChatService] 连接到聊天,主机:chat.quickblox.com,用户 JID:5434136-28329@chat.quickblox.com /7DE2CD1E-481F-4922-B21D-8EB14BF8B55F 2015-09-22 16:04:42.978 AppName[2263:96225] 登录状态:- 0
注意:-贬值
[[QBChat instance] sendMessage:message];
该代码已被弃用,并且您的 sdks 显示它选项,但也无法正常工作。
并设置其委托方法。
- (无效)chatDidConnect { }
- (void)chatDidAccidentallyDisconnect{ }
- (无效)chatDidReconnect{ }
- (void)chatDidReceiveMessage:(QBChatMessage *)message{ NSLog(@"收到消息%@",message); }
- (void)chatDidNotSendMessage:(QBChatMessage *)消息错误:(NSError *)error{ }
- (void)chatDidDeliverMessageWithID:(NSString *)messageID { }
但是,它只是调用了 1 个委托“chatDidConnect”,之后没有调用另一个委托。 在发送消息中我正在设置:
QBChatDialog *chatDialog = [[QBChatDialog alloc]initWithDialogID:[NSString stringWithFormat:@"%lu",(unsigned long)LoginUser.ID] type:QBChatDialogTypePrivate];
chatDialog.occupantIDs=@[@(SelectedChatUser.ID)];
chatDialog.name = @"school friends";
[QBRequest createDialog:chatDialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) {
_dialog=createdDialog;
QBChatMessage *message = [QBChatMessage message];
message.text = text;
message.senderID = senderId;
message.recipientID=createdDialog.recipientID;
message.deliveredIDs=[createdDialog.occupantIDs objectAtIndex:1];
message.dialogID=[NSString stringWithFormat:@"%@",createdDialog.ID];
message.senderNick=@"Nick Simulator";
message.dateSent = [NSDate date];
[self.items addObject:message];
[createdDialog sendMessage:message];
[self finishSendingMessageAnimated:YES];
} errorBlock:^(QBResponse *response) {
}];
但是 throws that code 消息没有发送,我们也没有收到任何消息 throwdelegate。
6) 之后我切换到新的方法来发送消息。写了如下代码发送消息:
- (void)didPressSendButton:(UIButton *)button
withMessageText:(NSString *)text
senderId:(NSUInteger)senderId
senderDisplayName:(NSString *)senderDisplayName
date:(NSDate *)date {
QBChatDialog *chatDialog = [[QBChatDialog alloc]initWithDialogID:[NSString stringWithFormat:@"%lu",(unsigned long)LoginUser.ID] type:QBChatDialogTypePrivate];
chatDialog.occupantIDs=@[@(SelectedChatUser.ID)];
chatDialog.name = @"school friends";
[QBRequest createDialog:chatDialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) {
_dialog=createdDialog;
QBChatMessage *message = [QBChatMessage message];
message.text = text;
message.senderID = senderId;
message.recipientID=createdDialog.recipientID;
message.deliveredIDs=[createdDialog.occupantIDs objectAtIndex:1];
message.dialogID=[NSString stringWithFormat:@"%@",createdDialog.ID];
message.senderNick=@"Nick Simulator";
message.dateSent = [NSDate date];
[self.items addObject:message];
[QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) {
NSLog(@"success: %@", createdMessage);
} errorBlock:^(QBResponse *response) {
NSLog(@"ERROR: %@", response.error);
}];
[self finishSendingMessageAnimated:YES];
} errorBlock:^(QBResponse *response) {
}];
【问题讨论】:
-
感谢 Mike 的详细问题。哪个代码每次都返回所有消息?这个 [QBRequest messagesWithDialogID..] ?