如今不少 App 都須要集成 IM 功能,今天就爲你們分享一下集成 IM 基本功能的步驟。本文內容以 JMessage 爲例。
極光 IM ( JMessage ) = 極光推送 ( JPush ) + IM,本篇只論述其中的 IM 部分,爲你們快速集成 IM 功能提供一個簡明的範例。html
咱們先來看一下 IM 的基本功能與本文內容的對應關係:
ios
#import <JMessage/JMessage.h>複製代碼
JMessage 核心頭文件。這是惟一須要導入到你的項目裏的頭文件,它引用了內部須要用到的頭文件。git
[JMessage addDelegate:self withConversation:nil];複製代碼
[JMessage setupJMessage:launchOptions
appKey:@"your appkey"
channel:@"channel name"
apsForProduction:NO
category:nil];複製代碼
application:didFinishLaunchingWithOptions:
[JMSGUser registerWithUsername:@"username"
password:@"password"
completionHandler:^(id resultObject, NSError *error) {
}];複製代碼
Parameters:api
[JMSGUser loginWithUsername:@"username"
password:@"password"
completionHandler:^(id resultObject, NSError *error) {
}];複製代碼
[JMSGConversation createSingleConversationWithUsername:@"username"
completionHandler:^(id resultObject, NSError *error) {
}];複製代碼
[(JMSGConversation*)resultObject sendTextMessage:@"text"];複製代碼
- (void)onReceiveMessage:(JMSGMessage *)message error:(NSError *)error{
if (message.content == kJMSGContentTypeText) {
NSString *text = ((JMSGTextContent *)message.content).text;
}
}複製代碼
message.contentType
判斷消息類型message.content
爲文本內容並獲取文本 text
以展現 UINSArray *messages = [(JMSGConversation*)resultObject messageArrayFromNewestWithOffset:nil limit:nil];複製代碼
resultObject
轉型後獲取JMSGConversation *conversation = [JMSGConversation singleConversationWithUsername:username];
[conversation clearUnreadCount];複製代碼
[JMSGConversation allConversations:^(id resultObject, NSError *error) {
}];複製代碼
BOOL success = [JMSGConversation deleteSingleConversationWithUsername:username];複製代碼
[JMSGUser userInfoArrayWithUsernameArray:nameArr completionHandler:^(id resultObject, NSError *error) {
}];複製代碼
[JMSGGroup createGroupWithName:name desc:desc memberArray:members completionHandler:^(id resultObject, NSError *error) {
}];複製代碼
[JMSGUser logout:^(id resultObject, NSError *error) {
}];複製代碼