Prepare
Proficiency in objective-c language
Have a mac computer, and installed xcode and cocoapods
Target
Teach you how to integrate instant messaging IM function in iOS application
The content is long, and you need to read it with peace of mind and patience, and be sure to refrain from impatience. The estimated time to read this article and connect according to this article is 2 hours
Register Appkey and username
This tutorial takes the integrated Huanxin IM as an example
Register a Huanxin account and log in to the console background:
Huanxin instant messaging cloud
The first step is to click Add Application:
The second step is to create an application
Example:
The third step is to find the appkey you just created, and click to view details
The fourth step is to create two users and add friends to each other
add friend:
At this point, the operation in the console part is completed, and an appkey and two users are obtained
appkey:1168171101115760#abc ,
username1: user1 ,
password1:1 ,
username2: user2 ,
password2: 1
Create a simple project to integrate IM and UI
Create a simple project
After the project is successfully created, use cocoapods to integrate Huanxin IM
Example:
final effect
The first part of Huanxin UI library integration method
Huanxin officially provides UI library
So how many ways do we integrate?
1. Use pod remote pull integration
2. Use pod to integrate local library
3. Drag directly into the project
In fact, this UI library is not the most ideal UI library, because in the process of actually doing the project, we need to make a lot of modifications to achieve the style that conforms to the product design. Therefore, the second and third methods are recommended here.
the first way
Reference: https://www.imgeek.net/video/76
the second way
Reference: https://www.imgeek.org/video/91
We'll demonstrate the third way here
The second part needs to be understood before integration
Q: Can the integrated UI library be used directly?
Answer: It should be noted here that in fact, the UI library only provides UI functions and does not provide logic parts, so it cannot be used directly.
Q: How should I use it? Where is the logic part? Do I still need to implement it myself?
Answer: No, we can take out the UI part we need from the official demo.
Q: So what do I need to do to integrate the UI as quickly as possible?
answer:
1. Download Demo: https://www.easemob.com/download/im
跑通Demo:https://www.imgeek.net/video/76
2. According to the situation of product design, choose the appropriate integration method from the three methods mentioned in the first part.
3. Take out the relevant interface logic from the demo and put it in your own project.
Part Three Getting Started
Here will demonstrate the third integration method
1. First of all, we have run through the demo, so the path of our current demo is as follows:
in:
EaseIM is a runnable project
EaseUI is the official UI library
2. First drag the UI part into the project
Delete the extra plist in it
Add in podfile
pod 'EMVoiceConvert', '0.1.0'
The effect is as follows:
At this point, the UI part is integrated.
The next step is to integrate the logic part, which needs to be extracted from the Demo
And add in podfile
Create a pch file (if there is one in the project, you don't need to create it)
Finally, configure project permissions (no configuration push is performed here)
So far, we have completed the integration of IM into the project.
Improve code and IM UI usage
In the first step, we create a helper responsible for configuration items and a class for callback processing of audio and video in the project
The class names are
EMAppConfig looks like this
EMAppCallHelper looks like this
The second step is perfected in appdelegate
Macro definition appkey (the definition already exists, the value of the definition needs to be modified)
(In addition, the location of adding or modifying the voice network id is here)
An example of use is as follows
Realize sending and receiving messages
The first part refers to the existing Demo writing method
1. Logical reference for sending messages
2. Callback for receiving messages
The second part, active implementation
How to actively send a message (the message here is to directly call the SDK to send a message)
//构建一个消息体
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"你好,环信"];
//构建一条消息
EMChatMessage *message = [[EMChatMessage alloc] initWithConversationID:"user2" body:body ext:@{}];
//设置消息的聊天类型为单聊消息
message.chatType = EMChatTypeChat;
//将消息发送出去
[EMClient.sharedClient.chatManager sendMessage:message progress:^(int progress) {
} completion:^(EMChatMessage * _Nullable message, EMError * _Nullable error) {
if(error){
NSLog(@"发送失败(%d):%@",error.code,error.errorDescription);
}else{
NSLog(@"发送成功");
}
}];
how to receive the message
Another: multiple copies of the agent can be added, but if the agent is not needed, it must be removed, otherwise it will be strongly referenced and cannot be released
So far, we have integrated IM and UI into the project. You're done!
Recommended reading: In-depth transformation of the cell in the chat interface: https://www.imgeek.org/video/121