最近項目中用到環(huán)信(項目中使用的是沒有音視頻的SDK),這邊就從導入SDK薯嗤、引入依賴庫后開始說起纤泵,網上找了些資料都是復制粘貼或者是比較早的,現在說說3.3.4的版本的一些使用玻褪。
注意:導入的SDK framework的status不需要設置為optional公荧,如果不添加Embedded Binaries稚矿,單獨修改status為optional可以解決報image no found的錯誤。集成后你會發(fā)現發(fā)送消息的時候messgae = nil桥爽,這個問題只需要添加一下Embedded Binaries昧识,如果把兩個 framework的status設置為optional運行跪楞、發(fā)消息都是OK的,上架的時候會出現問題上傳到蘋果后二進制文件無效缕碎。so池户,對導入SDK的設置為:添加Embedded Binaries校焦,不需要修改它framework的status。
導入的SDK以及設置Embedded Binaries后的截圖:
1氛雪、項目導入相關第三方
如果自己的項目中已經有引用MBProgressHUD报亩、SDWebImage(這個環(huán)信藏的比較深,也改成它自身的EMSDWebImage)赚楚、MJRefresh需要在環(huán)信的EMUIKit文件夾中找到并刪除骗卜,如果沒有刪除環(huán)信的EMSDWebImage在設置pch文件#ifdef OBJC #endif之后會五個錯誤寇仓。再刪除掉EMSDWebImage會報好幾個錯誤烤宙,因為環(huán)信修改了里面的一些方法躺枕,為了更方便環(huán)信那邊的使用,只需要找到對應的錯誤改回原本SD的方法就可以了罢猪。
2叉瘩、初始化(在AppDelegate.m文件中)
//注冊環(huán)信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亲轨、消息推送
消息推送,環(huán)信后臺添加證書已經制作推送證書這些就不細說了器虾,只說一點:環(huán)信后臺添加推送證書的時候需要輸入應用包名,應用包名是項目的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
{
//環(huán)信注冊 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
{
//消息通知點擊回調
}
消息推送過來后要顯示消息詳情需要設置它的推送昵稱,并且更新到服務器(環(huán)信的服務器):
[[EMClient sharedClient] setApnsNickname:response[@"nickname"]];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[EMClient sharedClient] updatePushOptionsToServer];
});
6葛圃、消息擴展(用于昵稱千扔、頭像)(也可以操作環(huán)信本地數據庫來實現)
自己的昵稱、頭像可以存儲本地库正,在登錄的時候存儲曲楚、修改的時候更新存儲,對方的頭像可以從自己的服務器后臺獲取到相應的數據褥符。
到環(huán)信UI的EaseMessageViewController控制器中龙誊,在環(huán)信的
- (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 編譯的特殊性,為了方便開發(fā)者使用粗截,IM將 i386 x86_64 armv7 arm64 幾個平臺都合并到了一起惋耙,所以使用動態(tài)庫上傳appstore時需要將i386 x86_64兩個平臺刪除后,才能正常提交審核
在SDK當前路徑下執(zhí)行以下命令刪除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/
具體操作:把環(huán)信demo里面的HyphenateSDK(實時音視頻版本:HyphenateFullSDK)的文件夾拖到桌面(或者隨意位置)然后打開終端 cd + 文件夾路徑(把文件夾路徑直接拉進終端)熊昌。然后執(zhí)行以上命令绽榛,完成后文件夾中會多出兩個文件:HyphenateLite_arm64、HyphenateLite_armv7這樣就分離成功婿屹,然后直接把HyphenateLite.framework拖進工程灭美,不是把整個文件夾拖進工程!0豪届腐!運行只能用真機,不支持模擬器蜂奸,可以自己替換回來犁苏,上架的時候再替換。
主要就這些吧扩所,別的細的一些東東就不一一說明了围详。