初始化YWAPI

  • YWAPI类是OpenIM所有接口的入口,使用单例来访问相关的方法,例如,按照下面的方式来初始化:
    /**
     *  初始化示例代码
     */
    - (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;
        }
    }

    注意: 您可以根据自身的代码结构,选择合适的时机进行初始化,例如,在AppDelegatedidFinishLaunchingWithOptions函数中。

获取并保持YWIMKit(或者YWIMCore)对象

  • 如上面的代码示例所示,YWAPI初始化成功后,我们获取并且保持了一个YWIMKit对象,该对象持有一个YWIMCore对象。

  • YWIMCore对象代表了使用OpenIM账号的上下文信息,可以从该对象中获得IM的各种服务。例如,您调用YWIMCore对象的登录服务登录成功后,可以调用YWIMCore对象的会话服务获取会话并发送消息。

注意:如果你没有集成IMSDK的UI层,而是只集成IMCore层,fetchIMKitForOpenIM方法会返回nil。你需要使用fetchNewIMCoreForOpenIM来获取YWIMCore实例。

获取Service

  • YWIMCore对象代表了一个OpenIM账号的上下文信息,可以从该对象中获取IM的各种服务。详细请查看YWIMCore.h。获取Service后,即可调用该Service的各个API。

  • 示例:

    id<IYWLoginService> loginService = [imkit.IMCore getLoginService];
    id<IYWConversationService> conversationService = [imkit.IMCore getConversationService];

常用的Service

  • IYWLoginService.h 登录及长连接相关
  • IYWConversationService.h 会话及消息相关
  • IYWTribeService.h 群相关接口
  • IYWContactService.h 黑名单、用户Profile、在线状态相关接口
  • 更多服务请关注YWIMCore.h中获取服务相关API

聊天对象: YWPerson

  • 您需要一个YWPerson聊天对象才能够发起单聊,例如使用下面的代码,创建一个聊天对象,并打开聊天页面:
    /// 打开单聊
    YWPerson *person = [[YWPerson alloc] initWithPersonId:@"uid10"];
    [self.ywIMKit openChatWithPerson:person fromController:self];

群: YWTribe

  • 您需要一个YWTribe对象才能够发起群聊,您可以使用如下的代码更新群列表
    /// 更新群列表
    [[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) {
    	/// 提示发送结果
    	;
    }];

会话对象: YWConversation

无论是单聊,还是群聊,在发起聊天之前,您都需要获得一个会话对象,例如:

  • 获取单聊会话

    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

所有消息都被抽象成IYWMessage接口,每一个消息都具有如下的基本属性:

  • messageId: 消息Id
  • messageFromPerson: 发送者
  • messageToPerson: 接收者(群聊消息该属性为nil)
  • messageSendStatus: 发送状态
  • conversationId: 所属会话Id
  • messageBody: 消息体
  • time: 消息发送时间
  • hasReaded: 是否已读

注意:目前有较多的接口,例如YWConversationfetchedObjects属性,用于监听新消息的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];
}

消息体:YWMessageBody

消息IYWMessage中有一个messageBody属性,表示了具体的消息内容,这是所有消息内容的基类。文本消息体、图片消息体等各种不同的消息体,都继承自YWMessageBody基类。

  • 当你接收到消息时,总是可以从messageBody中获取到对应的数据。
  • 当你需要发送消息时,总是需要先构造对应的messageBody,将数据放入body中。

判断消息类型

您可以直接判断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来表示。一般地,当您的消息列表显示这种消息体时,您可以提醒用户更新新版本。

FAQ

关于此文档暂时还没有FAQ
返回
顶部