iOS implements native IM development

foreword

The IM module natively developed by the iOS OC language is used in the case where the native development of IM is required in the project. It has complete UI capabilities such as sending text, expressions, voice, pictures, and videos. Since the back-end api was not external in the previous implementation, this is where A piece that did not do it myself. Contains functions such as picture preview and video playback. This project will be updated for a long time. If you have any questions, you can ask me, my email: [email protected] , I will solve it as soon as possible

Warehouse address: github.com/fshmjl/RCha…

The effect diagram is as follows

image

Project Introduction

Module functions such as input boxes are encapsulated in the project, which is convenient for reuse or rewriting

输入框模块              InputBox
输入框功能页面           KInputBoxView.m
输入框表情页面           KInputBoxViewCtrl.m
输入框更多页面           KInputBoxMoreView.m
输入框部分代理           KInputBoxViewDelegate
复制代码

Delegate provided by KInputBoxViewDelegate

@protocol KInputBoxViewDelegate <NSObject>

@optional
#pragma mark - 表情页面(KInputBoxEmojiView)代理

/**
点击选择表情

@param emojiView 表情所在页面
@param emojiDic 表情数据
@param emojiType 表情类型
*/
- (void)emojiView:(KInputBoxEmojiView *)emojiView
didSelectEmoji:(KEmojiModel *)emojiDic
emojiType:(KEmojiType)emojiType;


/**
删除光标前面的表情
*/
- (void)emojiViewDeleteEmoji;

/**
点击发送按钮,发送表情

@param emojiView 表情菜单
@param emojiStr 发送按钮
*/
- (void)emojiView:(KInputBoxEmojiView *)emojiView
sendEmoji:(NSString *)emojiStr;

#pragma mark - 表情菜单代理部分

/**
点击添加表情按钮

@param menuView 表情菜单
@param addBut 点击按钮
*/
- (void)emojiMenuView:(KInputBoxEmojiMenuView *)menuView
clickAddAction:(UIButton *)addBut;

/**
选择表情组

@param menuView 表情菜单页面
@param emojiGroup 表情组
*/
- (void)emojiMenuView:(KInputBoxEmojiMenuView *)menuView
didSelectEmojiGroup:(KEmojiGroup *)emojiGroup;

/**
点击发送按钮,发送表情

@param menuView 表情菜单
@param sendBut 发送按钮
*/
- (void)emojiMenuView:(KInputBoxEmojiMenuView *)menuView
sendEmoji:(UIButton *)sendBut;

#pragma mark - 输入框代理部分

/**
通过输入的文字的变化,改变输入框的高度

@param inputBox 输入框
@param height 改变的高度
*/
- (void)inputBox:(KInputBoxView *)inputBox changeInputBoxHeight:(CGFloat)height;

/**
发送消息

@param inputBox 输入框
@param textMessage 输入的文字内容
*/
- (void)inputBox:(KInputBoxView *)inputBox
sendTextMessage:(NSString *)textMessage;

/**
状态改变

@param inputBox 输入框
@param fromStatus 上一个状态
@param toStatus 当前状态
*/
- (void)inputBox:(KInputBoxView *)inputBox
changeStatusForm:(KInputBoxStatus)fromStatus
to:(KInputBoxStatus)toStatus;

/**
点击输入框更多按钮事件

@param inputBox 输入框
@param inputStatus 当前状态
*/
- (void)inputBox:(KInputBoxView *)inputBox
clickMoreInput:(KInputBoxStatus)inputStatus;
复制代码

session page

Needless to say, the session page is an ordinary UITableView. If you need to rewrite the session view, you only need to change its Cell (KMessagesListTableViewCell).

Highlight the chat page

As we all know, the chat page is also a UITableView. In fact, the real cumbersome chat page is the problem of different cells for different message types, and the problem of layout in the message page. Because there are many different cells on the message page, and the refresh is frequent, all need to be considered. For many UITableView optimization problems, such as layout problems, although AutoLayout has advantages in layout, it will degrade performance. Therefore, Frame layout is almost used in message pages and Cells in RChat, mainly to improve performance. Another problem is the calculation of the row height. It is a common problem for UITableView optimization. Our Cell needs to set the row height first, so it is generally necessary to cache the row height to avoid multiple calculations by the system. When writing this project, I also read several IM interface projects or demos, none of which are very complete and almost cannot be used directly, so I wrote this project, many things in the project can be rewritten, inherited and extended . The supported types of chat messages are text (including emoticons), pictures, videos, voice and mail. Other types need to be defined according to their own needs, but it is recommended to inherit KChatTableViewCell when defining them, which is convenient for unified processing.

消息基类                   KChatTableViewCell
文本消息(包含表情)         KChatTextTableViewCell
图片消息                   KChatImageTableViewCell
视频消息                   KChatVideoTableViewCell
语音消息                   KChatVoiceTableViewCell
邮件消息                   KChatMailTableViewCell
消息代理                   KChatTableViewCellDelegate
复制代码

Methods provided in KChatTableViewCellDelegate


@protocol KChatTableViewCellDelegate <NSObject>

/**
点击cell中的头像

@param tableViewCell 当前cell
@param messageModel 当前cell的数据
*/
- (void)chatTableViewCell:(KChatTableViewCell *)tableViewCell clickAvatarImageViewMessageModel:(KMessageModel *)messageModel;

/**
点击消息背景

@param tableViewCell 当前cell
@param messageModel 当前cell的数据
*/
- (void)chatTableViewCell:(KChatTableViewCell *)tableViewCell clickBackgroudImageViewMessageModel:(KMessageModel *)messageModel;

/**
当发送失败时点击,发送状态展示视图

@param tableViewCell 当前cell
@param conversationModel 会话信息
@param messageModel 消息
*/
- (void)chatTableViewCell:(KChatTableViewCell *)tableViewCell clickResendMessageWithConversationModel:(KConversationModel *)conversationModel messageModel:(KMessageModel *)messageModel;

/**
点击回复邮件

@param tableViewCell 当前cell
@param messageModel 当前cell的数据
*/
- (void)chatTableViewCell:(KChatMailTableViewCell *)tableViewCell replyMailMessageModel:(KMessageModel *)messageModel;

/**
点击回复全部

@param tableViewCell 当前cell
@param messageModel 当前cell的数据
*/
- (void)chatTableViewCell:(KChatMailTableViewCell *)tableViewCell
replyAllMaillMessageModel:(KMessageModel *)messageModel;

/**
点击转发邮件

@param tableViewCell 当前cell
@param messageModel 当前cell的数据
*/
- (void)chatTableViewCell:(KChatMailTableViewCell *)tableViewCell
transmitMailMessageModel:(KMessageModel *)messageModel;

/**
点击语音消息

@param tableViewCell 当前cell
@param messageModel 当前数据
*/
- (void)chatTableViewCell:(KChatVoiceTableViewCell *)tableViewCell clickVoiceMessageMessageModel:(KMessageModel *)messageModel;

复制代码

Chat message controller KChatViewController controller is developed using Category

页面定义和文本消息         KChatViewController
语音部分                 KChatViewController+Voice
图片和视频部分            KChatViewController+Image
复制代码

The message page provides several frequently used methods

// 重新刷新数据
- (void)reloadData;
// 移动到底部
- (void)scrollTableViewBottom ;
// 添加一条消息
- (void)addMessage:(KMessageModel *)model;
// 最后一条消息
- (KMessageModel *)lastMessage;

复制代码

To send a message, you need to construct a KMessageModel and call addMessage:, but the code that needs to be cached and uploaded to the server needs to be written according to your own needs.

If you have any questions or bugs in the project, you can send me Issues, describe the problem and reproduce steps clearly, and I will update it as soon as possible. If you need help, you can send an email to [email protected] , and finally please order Star.

Guess you like

Origin juejin.im/post/7096109475110584356