最近项目中用到环信(项目中使用的是没有音视频的SDK),这边就从导入SDK、引入依赖库后开始说起,网上找了些资料都是复制粘贴或者是比较早的,现在说说3.3.4的版本的一些使用。
注意:导入的SDK framework的status不需要设置为optional,如果不添加Embedded Binaries,单独修改status为optional可以解决报image no found的错误。集成后你会发现发送消息的时候messgae = nil,这个问题只需要添加一下Embedded Binaries,如果把两个 framework的status设置为optional运行、发消息都是OK的,上架的时候会出现问题上传到苹果后二进制文件无效。so,对导入SDK的设置为:添加Embedded Binaries,不需要修改它framework的status。
导入的SDK以及设置Embedded Binaries后的截图:
1、项目导入相关第三方
如果自己的项目中已经有引用MBProgressHUD、SDWebImage(这个环信藏的比较深,也改成它自身的EMSDWebImage)、MJRefresh需要在环信的EMUIKit文件夹中找到并删除,如果没有删除环信的EMSDWebImage在设置pch文件#ifdef OBJC #endif之后会五个错误。再删除掉EMSDWebImage会报好几个错误,因为环信修改了里面的一些方法,为了更方便环信那边的使用,只需要找到对应的错误改回原本SD的方法就可以了。
2、初始化(在AppDelegate.m文件中)
//注册环信SDK
EMOptions *options = [EMOptions optionsWithAppkey:EMkey];
options.apnsCertName = apnsCertName;
//初始化SDK
[[EMClient sharedClient] initializeSDKWithOptions:options];
//配置IM的UI key
[[EaseSDKHelper shareHelper] hyphenateApplication:application didFinishLaunchingWithOptions:launchOptions appkey:EMkey apnsCertName:apnsCertName otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
3、登录
EMError *error = [[EMClient sharedClient] loginWithUsername:hxName password:hxPsw];
if (!error) {
//登录成功后的操作
}
登录成功后,如果要设置自动登录代码如下:
[[EMClient sharedClient].options setIsAutoLogin:YES];
4、调起单聊界面
// CustomChatController继承EaseUI的EaseMessageViewController,如果不继承也行,直接调用EaseMessageViewController
CustomChatController * chatC = [[CustomChatController alloc] initWithConversationChatter:to conversationType:EMConversationTypeChat];
[self.navigationController pushViewController:chatC animated:YES];
5、消息推送
消息推送,环信后台添加证书已经制作推送证书这些就不细说了,只说一点:环信后台添加推送证书的时候需要输入应用包名,应用包名是项目的Bundle Identifier而不是应用程序名。注册代码如下:
iOS 10 之后需要添加#import <UserNotifications/UserNotifications.h>
//注册推送
EMPushOptions * pushOptions = [[EMClient sharedClient] pushOptions];
pushOptions.displayStyle = EMPushDisplayStyleMessageSummary;
[pushOptions setNoDisturbStatus:EMPushNoDisturbStatusClose];
//iOS10 注册APNs
if (NSClassFromString(@"UNUserNotificationCenter")) {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError *error) {
if (granted) {
#if !TARGET_IPHONE_SIMULATOR
[application registerForRemoteNotifications];
#endif
}
}];
return;
}
if([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
实现注册APNS回调:
//注册token
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//环信注册 Device Token
[[EMClient sharedClient] bindDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
//ios 10 之后回调
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"Userinfo %@",notification.request.content.userInfo);
//功能:可设置是否在应用内弹出通知
completionHandler(UNNotificationPresentationOptionAlert);}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
//消息点击回调
}
//iOS 10 之前回调
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//消息通知点击回调
}
消息推送过来后要显示消息详情需要设置它的推送昵称,并且更新到服务器(环信的服务器):
[[EMClient sharedClient] setApnsNickname:response[@"nickname"]];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[EMClient sharedClient] updatePushOptionsToServer];
});
6、消息扩展(用于昵称、头像)(也可以操作环信本地数据库来实现)
自己的昵称、头像可以存储本地,在登录的时候存储、修改的时候更新存储,对方的头像可以从自己的服务器后台获取到相应的数据。
到环信UI的EaseMessageViewController控制器中,在环信的
- (void)sendTextMessage:(NSString *)text; - (void)sendTextMessage:(NSString *)text withExt:(NSDictionary*)ext; - (void)sendLocationMessageLatitude:(double)latitude longitude:(double)longitude andAddress:(NSString *)address; - (void)sendImageMessageWithData:(NSData *)imageData; - (void)sendImageMessage:(UIImage *)image - (void)sendVoiceMessageWithLocalPath:(NSString *)localPath duration:(NSInteger)duration - (void)sendVideoMessageWithURL:(NSURL *)url
这些方法中添加上需要扩展的消息,自己的头像、昵称以及对方的头像、昵称,例如:
- (void)sendTextMessage:(NSString *)text
{
//消息扩展ext
NSDictionary *ext = @{@"fromNickname":_fromNickName,@"fromAvatar":_fromAvatar,@"toNickname":_toNickName,@"toAvatar":_toAvatar};
if (self.conversation.type == EMConversationTypeGroupChat) {
NSArray *targets = [self _searchAtTargets:text];
if ([targets count]) {
__block BOOL atAll = NO;
[targets enumerateObjectsUsingBlock:^(NSString *target, NSUInteger idx, BOOL *stop) {
if ([target compare:kGroupMessageAtAll options:NSCaseInsensitiveSearch] == NSOrderedSame) {
atAll = YES;
*stop = YES;
}
}];
if (atAll) {
ext = @{kGroupMessageAtList: kGroupMessageAtAll};
}
else {
ext = @{kGroupMessageAtList: targets};
}
}
}
[self sendTextMessage:text withExt:ext];
}
在聊天列表中获取对应的扩展消息:
id<IConversationModel> model = IMModel;
NSDictionary * ext = model.conversation.lastReceivedMessage.ext;
7、上架
由于 iOS 编译的特殊性,为了方便开发者使用,IM将 i386 x86_64 armv7 arm64 几个平台都合并到了一起,所以使用动态库上传appstore时需要将i386 x86_64两个平台删除后,才能正常提交审核
在SDK当前路径下执行以下命令删除i386 x86_64两个平台
实时音视频版本Hyphenate.framework
lipo Hyphenate.framework/Hyphenate -thin armv7 -output Hyphenate_armv7
lipo Hyphenate.framework/Hyphenate -thin arm64 -output Hyphenate_arm64
lipo -create Hyphenate_armv7 Hyphenate_arm64 -output Hyphenate
mv Hyphenate Hyphenate.framework/
不包含实时音视频版本HyphenateLite.framework
lipo HyphenateLite.framework/HyphenateLite -thin armv7 -output HyphenateLite_armv7
lipo HyphenateLite.framework/HyphenateLite -thin arm64 -output HyphenateLite_arm64
lipo -create HyphenateLite_armv7 HyphenateLite_arm64 -output HyphenateLite
mv HyphenateLite HyphenateLite.framework/
具体操作:把环信demo里面的HyphenateSDK(实时音视频版本:HyphenateFullSDK)的文件夹拖到桌面(或者随意位置)然后打开终端 cd + 文件夹路径(把文件夹路径直接拉进终端)。然后执行以上命令,完成后文件夹中会多出两个文件:HyphenateLite_arm64、HyphenateLite_armv7这样就分离成功,然后直接把HyphenateLite.framework拖进工程,不是把整个文件夹拖进工程!!!运行只能用真机,不支持模拟器,可以自己替换回来,上架的时候再替换。
主要就这些吧,别的细的一些东东就不一一说明了。