/**
* 初始化示例代码
*/
- (BOOL)exampleInit;
{
/// 设置环境
[[YWAPI sharedInstance] setEnvironment:YWEnvironmentRelease];
/// 开启日志
[[YWAPI sharedInstance] setLogEnabled:YES];
NSLog(@"SDKVersion:%@", [YWAPI sharedInstance].YWSDKIdentifier);
NSError *error = nil;
/// 异步初始化IM SDK
[[YWAPI sharedInstance] syncInitWithOwnAppKey:@"23015524" getError:&error];
if (error.code != 0 && error.code != YWSdkInitErrorCodeAlreadyInited) {
/// 初始化失败
return NO;
} else {
if (error.code == 0) {
/// 首次初始化成功
/// 获取一个IMKit并持有
self.ywIMKit = [[YWAPI sharedInstance] fetchIMKitForOpenIM];
} else {
/// 已经初始化
}
return YES;
}
} 注意: 您可以根据自身的代码结构,选择合适的时机进行初始化,例如,在AppDelegate的didFinishLaunchingWithOptions函数中。
如上面的代码示例所示,YWAPI初始化成功后,我们获取并且保持了一个YWIMKit对象,该对象持有一个YWIMCore对象。
YWIMCore对象代表了使用OpenIM账号的上下文信息,可以从该对象中获得IM的各种服务。例如,您调用YWIMCore对象的登录服务登录成功后,可以调用YWIMCore对象的会话服务获取会话并发送消息。
注意:如果你没有集成IMSDK的UI层,而是只集成IMCore层,fetchIMKitForOpenIM方法会返回nil。你需要使用fetchNewIMCoreForOpenIM来获取YWIMCore实例。
YWIMCore对象代表了一个OpenIM账号的上下文信息,可以从该对象中获取IM的各种服务。详细请查看YWIMCore.h。获取Service后,即可调用该Service的各个API。
示例:
id<IYWLoginService> loginService = [imkit.IMCore getLoginService]; id<IYWConversationService> conversationService = [imkit.IMCore getConversationService];
YWIMCore.h中获取服务相关API/// 打开单聊 YWPerson *person = [[YWPerson alloc] initWithPersonId:@"uid10"]; [self.ywIMKit openChatWithPerson:person fromController:self];
/// 更新群列表
[[self.ywIMCore getTribeService] requestAllTribesFromServer:^(NSArray *tribes, NSError *error) {
self.arrayTribeList = [NSArray arrayWithArray:tribes];
}];
...
/// 发送群聊消息
YWTribe *tribe = [self.arrayTribeList objectAtIndex:0];
YWTribeConversation *conversation = [YWTribeConversation fetchConversationByTribe:tribe createIfNotExist:YES baseContext: self.ywIMCore];
[conversation asyncSendMessageBody:[[YWMessageBodyText alloc] initWithMessageText:self.textFieldTextToSend.text] progress:^(CGFloat progress, NSString *messageID) {
/// 展示发送进度
;
} completion:^(NSError *error, NSString *messageID) {
/// 提示发送结果
;
}]; 无论是单聊,还是群聊,在发起聊天之前,您都需要获得一个会话对象,例如:
获取单聊会话
YWPerson *person = [[YWPerson alloc] initWithPersonId:@"uid10"]; YWP2PConversation *conversation = [YWP2PConversation fetchConversationByPerson:person creatIfNotExist:YES baseContext: self.ywIMCore];
获取群聊会话
YWTribe *tribe = [self.arrayTribeList objectAtIndex:0]; YWTribeConversation *conversation = [YWTribeConversation fetchConversationByTribe:tribe createIfNotExist:YES baseContext: self.ywIMCore];
所有消息都被抽象成IYWMessage接口,每一个消息都具有如下的基本属性:
messageId: 消息IdmessageFromPerson: 发送者messageToPerson: 接收者(群聊消息该属性为nil)messageSendStatus: 发送状态conversationId: 所属会话IdmessageBody: 消息体time: 消息发送时间hasReaded: 是否已读注意:目前有较多的接口,例如YWConversation的fetchedObjects属性,用于监听新消息的YWConversationOnNewMessageBlockV2回调等等,其数组中包含的是内部的WXOMessageObject对象,实际上遵循的是IYWMessage协议
你可以通过如下的方式,获取消息数据:
/**
* 监听新消息
*/
- (void)exampleListenNewMessage
{
[[self.ywIMKit.IMCore getConversationService] addOnNewMessageBlockV2:^(NSArray *aMessages, BOOL aIsOffline) {
/// 你可以在此处根据需要播放提示音或者tips
[aMessages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id<IYWMessage> message = obj;
if ([[message messageBody] isKindOfClass:[YWMessageBodyText class]]) {
/// 提示收到文本消息
} else {
/// 你可以自己决定根据不同的类型,提示不同的文案
}
}];
} forKey:self.description ofPriority:YWBlockPriorityDeveloper];
}
消息IYWMessage中有一个messageBody属性,表示了具体的消息内容,这是所有消息内容的基类。文本消息体、图片消息体等各种不同的消息体,都继承自YWMessageBody基类。
您可以直接判断messageBody的类型,来判断消息具体是什么类型,例如:
if ( [[message messageBody] isKindOfClass:[YWMessageBodyText class]] )
{
// show text
}
else if ( [[message messageBody] isKindOfClass:[YWMessageBodyImage class]] )
{
// show image
}
文本消息体YWMessageBodyText包含了一串文本消息messageText。
在多聊中发送文本消息示例:
YWTribeConversation *conversation = [YWTribeConversation fetchConversationByTribe:self.tribeSelected createIfNotExist:YES baseContext: self.ywIMCore];
/// 构造文本消息体并发送消息
[conversation asyncSendMessageBody:[[YWMessageBodyText alloc] initWithMessageText:self.textFieldTextToSend.text] progress:^(CGFloat progress, NSString *messageID) {
;
} completion:^(NSError *error, NSString *messageID) {
if (error.code == 0) {
/// show message
} else {
/// show error
}
}];
您可以使用UIImage对象创建图片消息体YWMessageBodyImage,示例如下:
__weak typeof(self) weakSelf = self;
YWMessageBodyImage *imageMessageBody = [[YWMessageBodyImage alloc] initWithMessageImage:image];
[self.conversation asyncSendMessageBody:imageMessageBody progress:^(CGFloat progress, NSString *messageID) {
/// show progress
} completion:^(NSError *error, NSString *messageID) {
/// show result
}];
您可以使用包含语音数据的NSData对象和语音时长来创建语音消息体YWMessageBodyVoice,示例如下:
YWMessageBodyVoice *bodyVoice = [[YWMessageBodyVoice alloc] initWithMessageVoiceData:wavData
duration:nRecordingTime];
[self.conversation asyncSendMessageBody:bodyVoice progress:nil completion:NULL];
您可以使用地理坐标和位置名称创建地理位置消息体YWMessageBodyLocation,示例如下:
YWMessageBodyLocation *messageBody = [[YWMessageBodyLocation alloc] initWithMessageLocation:location locationName:name];
if (messageBody) {
[weakController.conversation asyncSendMessageBody:messageBody progress:nil completion:NULL];
}
自定义消息体YWMessageBodyCustomize允许您发送包含您的自定义数据的消息,您需要使用自定义消息内容和摘要,来构造消息体,示例如下:
YWMessageBodyCustomize *messageBody = [[YWMessageBodyCustomize alloc] initWithMessageCustomizeContent:self.contentTextView.text summary:self.summeryTextView.text];
[self.conversation asyncSendMessageBody:messageBody
progress:nil
completion:^(NSError *error, NSString *messageID) {
/// show result
}];
您可能在一个老版本中收到新版本的消息体数据,对于这种老版本SDK尚未支持的消息体,统一使用YWMessageBodyUnsupported来表示。一般地,当您的消息列表显示这种消息体时,您可以提醒用户更新新版本。