标签:聊天 ios开发 uitableview 线程 继承
话不多说,先看一下做好的聊天软件界面:
首先在StoryBoard里拖了一个UItableView和一个view用来输入文字或者语音,右边的按钮用来切换文字和语音:
<strong> </strong><span style="font-size:14px;"> //初始换录音 [self initRecord]; //获取用户头像 [self getHeadImg]; //获取订单详情 [self getOrderDetailInfo];</span>
<span style="font-size:14px;">#pragma mark - 轮询消息 - (void)runLoopMessage { SpeakType speaker = [YCChatInfo getSpeakerPassengerBy:self.orderInfo.bigType]; [[YCReceiveMessageCenter defaultMessageCenter] getMessageListBySpeaker:speaker isRemote:NO andPushId:nil]; }</span>第一句代码是获取会话类型,这里定义了一个枚举值,主要有以下几个角色:
//会话类型 enum SpeakType { //Business YCDriverType = 11, YCPassenger = 12, YCSystem = 13, //v5.2.2 增加系统角色 YCLoctionAutoReply = 14, //v5.2.2添加本地自动回复 YCDriverAutoReply = 15, //v5.2.2添加司机自动回复 司机角色 YCLoctionUpdateVersionReply = 16, //v5.2.2 未知消息类型回复 【提示不支持的消息类型。请升级】 }; typedef NSInteger SpeakType;然后根据会话类型去请求聊天list接口,然后请求成功后对数据进行处理:
//isRemote 点击推送栏消息 if (response && [response[@"ret_code"] integerValue] == 200) { NSArray *array = response[@"result"]; id topVC = [[YCAppDelegate sharedDelegate] topViewController]; __block NSString *orderID = nil; __block NSString *dType = nil; [array enumerateObjectsUsingBlock:^(NSDictionary *result, NSUInteger idx, BOOL *stop) { NSString *type = [self controlMessageDispatch:result]; dType = type; <span style="white-space:pre"> </span>//此处省略五百字 } } else { DLog(@"轮询数据失败 response = %@, error = %@", response, error); }
typedef NS_ENUM(NSInteger, ClassType) { OrderClass = 1, ChatClass = 2, UserClass = 3, };
- (void)receiveChatMessage:(id)object方法,然后有一个消息状态字段kChatRepeatState,如果kChatRepeatState == 20,表示已读消息,直接返回。如果不是,用content初始化YCChatInfo:
NSDictionary *content = dic[@"content"]; YCChatInfo *item; item = [[YCChatInfo alloc] initWithDictionary:content];然后去判断ChatType,有以下几种:
//聊天 类型 enum ChatType { ChatText = 1, ChatImage = 2, ChatAudio = 3, ChatPOI = 4, ChatMix = 5, //混合内容 ChatCard = 6,//v5.2.2卡片消息 ChatUpdateHint = 701 //v5.2.2不支持类型升级提示 }; typedef NSInteger ChatType;
[[NSNotificationCenter defaultCenter] postNotificationName:kChatMessageNotification object:nil userInfo:@{@"chatInfo" : chatInfo}]; [self.chatStore insertChat:chatInfo];这里上次有个bug,聊天消息去重之后一直收不到语音消息,就是因为我先把新聊天消息先插入数据,然后再去发通知,导致往界面上显示聊天消息是总是显示不上去。
//首先判断是否是推送消息, 且判断该条点击的推送id进入的 if (isRemote && [pushId isEqualToString:result[@"id"]]) { if (!orderID && ([type isEqualToString:@"new_chat"] || [type isEqualToString:@"DRIVER_ARRIVE"] || [type isEqualToString:@"RECEPTION_DRIVER"]|| [type isEqualToString:@"SERVICE_DONE"])) { if ([type isEqualToString:@"new_chat"]) { orderID = result[@"content"][@"topic"]; } else if (![topVC isKindOfClass:[YCSelectDriverViewController class]] && ([type isEqualToString:@"DRIVER_ARRIVE"] || [type isEqualToString:@"RECEPTION_DRIVER"] || [type isEqualToString:@"SERVICE_DONE"])) { orderID = result[@"content"][@"order_id"]; } } } }]; if (orderID) { if ([DefaultValueForKey(kShowWelcome) boolValue]) { if (![topVC isKindOfClass:[YCWelcomeVC class]]) { [[NSNotificationCenter defaultCenter] postNotificationName:kRemotePushVC object:nil userInfo:@{@"orderID" : orderID, @"type":dType}]; } } }
- (void)receiveMessage:(NSNotification *)notification { YCChatInfo *chatInfo = notification.userInfo[@"chatInfo"]; //如果此时来的消息不输入当前会话 页面不进行操作 NSString *string1 = [[NSString alloc] initWithFormat:@"%@", chatInfo.orderID]; NSString *string2 = [[NSString alloc] initWithFormat:@"%@", self.orderInfo.serverOrderId]; if (![string1 isEqualToString:string2]) { return ; } NSInteger messageID = chatInfo.messageID; YCChatStore *chatStore = [[YCChatStore alloc]init]; BOOL isNotRepeat = [chatStore selectDBwithMessageID:messageID]; if (!isNotRepeat) { return; } [self insertRowToTableViewByIndexPath:chatInfo isSend:NO]; }第一句话是获取通知里传的聊天消息内容,然后拿chatinfo里的orderID和通过获取订单详情的接口里获得的orderID做比较,如果orderID不一致,直接return。如果一直就去YCChatStore里的selectDBwithMessageID方法里查询数据库是否有相同的messageID存在,如果存在直接return,如果不存在就插入界面
- (NSIndexPath *)insertRowToTableViewByIndexPath:(YCChatInfo *)chatInfo isSend:(BOOL)isSend { NSIndexPath *indexPath; [self.chatArray addObject:chatInfo]; indexPath = [NSIndexPath indexPathForRow:[self.chatArray count] - 1 inSection:0]; void(^ScrollBlock)() = ^{ DLog(@"%d",indexPath.row); [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; }; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:YCTableViewRowAnimationFromBottom completion:^{ if (isSend) { ScrollBlock(); } }]; if (!isSend) { ScrollBlock(); } return indexPath; }
iOS开发利用系统推送Notifaction和轮询实现简单聊天系统,布布扣,bubuko.com
iOS开发利用系统推送Notifaction和轮询实现简单聊天系统
标签:聊天 ios开发 uitableview 线程 继承
原文地址:http://blog.csdn.net/crazyzhang1990/article/details/38372885