初始化
在需要使用IMSDK的类中导入头文件
#import <IMSDK/IMClient.h>
注册APP,appName用于区分APNS推送证书
[[IMClient sharedClient] registerAppName:@"APPName"];
// APP进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[IMClient sharedClient] applicationDidEnterBackground:application UnreadCount:@"未读数"];
}
// APP将要从后台返回
- (void)
applicationDidBecomeActive:(UIApplication *)application{ [[IMClient sharedClient] applicationWillEnterForeground:application];}
连接服务器
APP请求得到IM分配的用户ID和token进行登录,调用连接方法登录IM。此方法只需调用一次,在网络变化及APP从后台回到前台等情况下SDK均会自动重连。
[[IMClient sharedClient] imConnectWithUserId:@"840" Token:@"GAWC9H4DsELihwKUKkn6WzGFMd.zr.NyCJoUEkHF2W8ZOvd927WXewZcjuHZbd79" ServerHost:IM_HOST ServerPort:IM_TCP_PORT Success:^(id user) {
} failure:^(NSError *error) {
}];
自行注册完成远程推送后,获取到deviceToken后与用户绑定
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[IMClient sharedClient] bindDeviceToken:deviceToken];
}
消息
消息实体类IMMessage,目前提供的消息类型有:文本消息,语音消息,图片消息,评价消息,商品消息,若需增加新类型,可新建消息类继承于IMMessage。
构造文本消息
IMMessage *message = [[IMMessage alloc] initMessageWithText:@"难以忘记初次见你"];
message.senderID = @"840";//发送者ID
message.recevierID = @"816";//接收者ID
图片消息
IMMessage *imageMessage = [[IMMessage alloc] initMessageWithFullImage:@"我是大图" PreviewImage:@"我是小图" ImageSize:CGSizeMake(100, 100)
];
imageMessage.senderID = @"840";
imageMessage.recevierID = @"816";
语音消息
IMMessage *audioMessage = [[IMMessage alloc] initMessageWithAudio:@"文件名" Duration:12];
audioMessage.senderID = @"840";
audioMessage.recevierID = @"816";
评价消息
IMMessage *evalMessage = [[IMMessage alloc] initMessagewithEvaluation:EvaluationResult_OK];
evalMessage.senderID = @"840";
evalMessage.recevierID = @"816";
商品消息
IMMessage *commdityMessage = [[IMMessage alloc] initMessagewithCommondity:@"商品连接或ID(待定)"];
commdityMessage.senderID = @"840";
commdityMessage.recevierID = @"816";
所有消息 senderID和recevierID都为必要参数,否则无法成功发送消息。
发送消息
[[IMClient sharedClient] sendMessage: message Success:^(NSString *time) {
} failure:^(NSError *error) {
}];
消息发送成功后,返回消息到达服务器的时间,可用于更新本地消息时间。
接收消息
在需要监听消息的类中设置代理
[[IMClient sharedClient] setReceiveMessageDelegate:self];
监听接收消息:
- (void)onReceived:(id)message PushMessageType:(YGHandlePushMessageType)pushMessageType {
//处理你的消息
}
拉取离线消息(目前无需手动调用)
连接成功后可直接调用- (void)getOfflineMessages拉取离线消息
[[IMClient sharedClient] getOfflineMessages];
离线消息拉取成功会直接调用上述的监听方法。
注销登录
/**
* 退出登录
*/
- (void)sendOfflineBackgroundWithUnreadCount:(NSInteger)count;