由於最近接觸到的專案需要用到聊天功能,關於聊天的SDK跟安卓同事統一,最終選擇了環信。在官方下載好SDK,並研究了一波,最終實現自定義聊天功能。
###實現前準備
1.下載環信SDK,我使用的版本是V3.2.0 2016-10-15
,並且一開始我只使用了簡版的,沒有實時語音視訊通訊,沒有支付寶紅包。基於環信實現實時語音視訊通話功能
2.在下載的資料夾中找到HyphenateSDK
、EaseUI
並在demo中找到emotion
表情包,一起拖到新工程中。
3.向Build Phases → Link Binary With Libraries 中新增依賴庫。SDK 依賴庫有:
CoreMedia.framework
AudioToolbox.framework
AVFoundation.framework
MobileCoreServices.framework
ImageIO.framework
libc++.dylib
libz.dylib
libstdc++.6.0.9.dylib
libsqlite3.dylib
複製程式碼
SDK 不支援 bitcode,向 Build Settings → Linking → Enable Bitcode 中設定 NO。在Build Settings-->Other Linker Flags-->雙擊 填寫上:-all_load
4.在工程中設定EaseUI-Prefix.pch
的路徑。Build Settings-->Prefix Header 複製貼上路徑。如果你工程中有.pch檔案,那麼就只需要將EaseUI-Prefix.pch
中的程式碼複製進原有的pct檔案中就行。(我就只複製了這些)
#define DEMO_CALL 1
#import <UIKit/UIKit.h>
#import <Availability.h>
#import <Foundation/Foundation.h>
#endif
複製程式碼
5.在AppDelegate.m
中配置環信SDK相關程式碼。首先在環信後臺註冊專案,並提交測試版的遠端推送證書anps,需要提供的是.p12檔案。(推送證書及p12相關,請自行百度)匯入標頭檔案#import "EMSDK.h"
在AppDelegate.m
中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}
方法中初始化環信相關配置。
//自動同意加好友.
BOOL accept = [[EMClient sharedClient].options isAutoAcceptFriendInvitation];
accept = YES;
//iOS8 註冊APNS
if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
[application registerForRemoteNotifications];
UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
else{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
}
//AppKey:註冊的AppKey,詳細見下面註釋。
//apnsCertName:推送證書名(不需要加字尾),詳細見下面註釋。
NSString *apnsCertName = nil;
#if DEBUG
apnsCertName = @"xxxx";//填寫自己的推送證書名字
#else
apnsCertName = @"XXXX";//同上
#endif
EMOptions *options = [EMOptions optionsWithAppkey:@"AppKey"];//填寫自己註冊的AppKey
options.apnsCertName = apnsCertName;//
[[EMClient sharedClient] initializeSDKWithOptions:options];
複製程式碼
並加上如下程式碼
#pragma mark - 獲取及上傳deviceToken
/*!
@method 系統方法
@abstract 系統方法,獲取deviceToken,並上傳環信
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[[EMClient sharedClient] bindDeviceToken:deviceToken];
}
/*!
@method 系統方法
@abstract 系統方法,獲取deviceToken 失敗
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"error -- %@",error);
}
#pragma mark - 進入前後臺 (環信)
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[EMClient sharedClient] applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[EMClient sharedClient] applicationWillEnterForeground:application];
}
複製程式碼
具體的SDK匯入過程也就這些,編譯一下,如果沒報錯,就匯入成功了。有報錯也沒關係,找到報錯的地方,有時候就是標頭檔案找不到,或者檔案重複。仔細看看,把不需要的刪掉,出現了連線錯誤就複製百度。放緩心態,要相信SDK是一定能匯入成功的。
###分析需求(一) 我沒有用環信demo的UI,大部分是自定義的。因為我覺得自定義的好一些,別人做的再好,那也是別人根據自己的需求做的,不一定滿足我們的需求。專案工程不便上傳,所以我就根據功能點,一步步地往下寫。(根據功能點來,更方便自定義) 從上圖中可以看到需要做到的一些功能點有:頭像、名稱、會話列表、訊息、訊息傳送的時間、未讀訊息標記;還有一點就是會話列表能左劃刪除。這六點中名稱跟頭像環信是不提供的,註冊過環信的都知道,它的ID是數字跟字母的組合,沒有像這樣顯示自定義名稱的。關於頭像,環信伺服器也是不提供儲存的。 ###實現相關功能(一) 1.登入或註冊環信賬號,獲取會話列表。(賬號密碼是按照我的專案來的,通過本地資料庫儲存好友資訊,對照著替換)首先匯入標頭檔案#import "EMSDK.h"
、#import "EaseUI.h"
/*!
@method 登入並載入會話列表。
@abstract 登入並載入會話列表。
*/
- (void)loginAndLoadData
{
//-----------------------登入邏輯----------------------//
FDAccountItem *item = [FDLoginTool fd_account];
NSString *psw = [item.key substringFromIndex:12];
NSLog(@"psw=%@==%@",psw,item.perName);
//---首先判斷能不能以該身份證為賬號進行登入----//
EMError *error = [[EMClient sharedClient] loginWithUsername:[item.key md5String] password:[psw md5String]];
if (!error) {
//設定遠端推送使用者名稱字
[[EMClient sharedClient] setApnsNickname:item.perName];
if (!error) {
NSLog(@"新增成功");
}
NSLog(@"登入成功");
//自動登入
[[EMClient sharedClient].options setIsAutoLogin:YES];
[MBProgressHUD hideHUD];
}
else
{
NSLog(@"%@", error);
//-------不能以該身份證號為賬號登入,就已該身份證號進行註冊-------//
EMError *regError = [[EMClient sharedClient]registerWithUsername:[item.key md5String] password:[psw md5String]];
if (regError ==nil) {
//-----註冊成功之後就進行登入-----//
EMError *logError = [[EMClient sharedClient] loginWithUsername:[item.key md5String] password:[psw md5String]];
if (!logError) {
//自動登入
[[EMClient sharedClient].options setIsAutoLogin:YES];
[MBProgressHUD hideHUD];
}
else
{
NSLog(@"登入失敗");
[MBProgressHUD hideHUD];
}
}
else
{
NSLog(@"error=%@",error);
[MBProgressHUD hideHUD];
}
}
//-------------------------end---------------------------//
//獲取所有會話
NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
self.dataArr = [[NSMutableArray alloc]initWithArray:conversations];
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];//聊天模組註冊監聽
}
複製程式碼
/*!
@method 環信方法,監聽收到訊息
@abstract 不管app前臺、後臺,收到訊息都會來到這個方法
*/
- (void)messagesDidReceive:(NSArray *)aMessages
{
[self getUnReadCountFromEM];
[self.tableView reloadData];
}
/*!
@method 從環信獲取所有的未讀訊息
@abstract 從環信獲取所有的未讀訊息
*/
- (NSInteger)getUnReadCountFromEM {
NSInteger unReadCount = 0;
// 獲取所有回話列表
NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
self.dataArr = [NSMutableArray arrayWithArray:conversations];
for (EMConversation *conversation in conversations) {
unReadCount += conversation.unreadMessagesCount;
}
if (unReadCount > 0) {
[self.tabBarController.tabBar huanX_showBadgeOnItemIndex:0];//自定義介面上顯示未讀訊息小紅點
}
else
{
[self.tabBarController.tabBar fd_hideBadgeOnItemIndex:0];//同上
}
return unReadCount;
}
複製程式碼
2.單元格cell介面的配置以及訊息的處理。
XiaoXiTableViewCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"XiaoXiTableViewCell" owner:nil options:nil]lastObject];
EMConversation *conversation = self.dataArr[indexPath.row];
int a = conversation.unreadMessagesCount;
if (a > 0) {
cell.hongDianImage.hidden = NO;
}else
{
cell.hongDianImage.hidden = YES;
}
//獲得最後一條訊息
EMMessage *message = conversation.latestMessage;
//獲得訊息體
EMMessageBody *messageBody = message.body;
NSString *str;
if (messageBody.type==EMMessageBodyTypeText) {
//訊息為文字時,強轉為EMTextMessageBody 獲取文字訊息
EMTextMessageBody *textBody =(EMTextMessageBody *)messageBody;
str = [EaseConvertToCommonEmoticonsHelper convertToSystemEmoticons:textBody.text];
//str = textBody.text;
}
//判斷訊息型別,輸出響應型別
else if (messageBody.type==EMMessageBodyTypeImage)
{
str = @"[圖片]";
}
else if (messageBody.type==EMMessageBodyTypeVoice)
{
str = @"[語音]";
}
else if (messageBody.type==EMMessageBodyTypeLocation)
{
str = @"[位置]";
}
else if (messageBody.type==EMMessageBodyTypeVideo)
{
str = @"[視訊]";
}
else if (messageBody.type==EMMessageBodyTypeFile)
{
str = @"[檔案]";
}
else
{
str = @"";
}
cell.xiaoXiLabel.text = str;
//-----------------end--------------------//
//找到會話
EaseConversationModel *model = [[EaseConversationModel alloc]initWithConversation:conversation];
//SaveHaoYouModel *haoYouModel = [HaoYouDao selectedFromPeopleTableWithMDsfz:model.title];
//設定訊息時間
cell.timeLabel.text = [self timeStr:message.timestamp];
//設定訊息傳送者暱稱
// 開啟資料庫
[HaoYouDao createYiShengTable];
// 搜尋名醫模型
SaveMingYiModel *mYModel = [HaoYouDao selectedFromPeopleTableWithMDsfz:model.title];
NSLog(@"ss%@----%@-",mYModel.docName,mYModel.idcardno);
cell.nameLabel.text = mYModel.docName;
NSString *imageUrl = [NSString stringWithFormat:@"https://219.140.163.100:2265/FDAPP/app_upload/%@.jpg",mYModel.idcardno];
[cell.headImage sd_setImageWithURL:[NSURL URLWithString:imageUrl]
placeholderImage:[UIImage imageNamed:@"touxiang"]];
//通過擴充套件訊息獲得,該訊息的擴充套件訊息,即使用者的頭像,暱稱
//統一設定單元格分割線,和單元格選中樣式
cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0);
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
複製程式碼
3.時間的處理。
/*!
@method 計算訊息時間。
@abstract 計算接收到訊息的時間,並判斷。
*/
- (NSString *)timeStr:(long long)timestamp
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
// 獲取當前時間的年、月、日
NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
NSInteger currentYear = components.year;
NSInteger currentMonth = components.month;
NSInteger currentDay = components.day;
NSDate *msgDate = nil;
// 獲取訊息傳送時間的年、月、日
if (timestamp == 0) {
msgDate = currentDate;
} else {
msgDate = [NSDate dateWithTimeIntervalSince1970:timestamp/1000.0];
}
components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
CGFloat msgYear = components.year;
CGFloat msgMonth = components.month;
CGFloat msgDay = components.day;
// 判斷
NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {
//今天
dateFmt.dateFormat = @"HH:mm";
}else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-1 == msgDay ){
//昨天
dateFmt.dateFormat = @"昨天 HH:mm";
}else{
//昨天以前
dateFmt.dateFormat = @"yyyy-MM-dd";
}
return [dateFmt stringFromDate:msgDate];
}
複製程式碼
4.刪除會話。
/*!
@method 開啟tableview刪除功能。
@abstract 開啟tableview刪除功能.
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 刪除會話
EMConversation *conversation = self.dataArr[indexPath.row];
[[EMClient sharedClient].chatManager deleteConversation:conversation.conversationId isDeleteMessages:YES completion:^(NSString *aConversationId, EMError *aError) {
}];
// 重新整理表
[self.dataArr removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}
複製程式碼
5.點選訊息,與相應的id進行聊天。
//點選訊息,與相應的id進行聊天
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//找到會話
EMConversation *conversation = self.dataArr[indexPath.row];
EaseConversationModel *model = [[EaseConversationModel alloc]initWithConversation:conversation];
//從本地資料庫中匹配,找到好友資訊
SaveMingYiModel *mingYiModel = [HaoYouDao selectedFromPeopleTableWithMDsfz:model.title];
//model.title = haoYouModel.name;
EaseMessageViewController *viewController = [[EaseMessageViewController alloc] initWithConversationChatter:model.conversation.conversationId conversationType:model.conversation.type];
// viewController.title = model.title;
viewController.title = mingYiModel.docName;
[self.navigationController pushViewController:viewController animated:YES];
}
複製程式碼
###分析需求(二)
這個介面的UI大部分都已經有了,需要改的是導航控制器標題、暱稱、頭像。 ###實現相關功能(二) 1.標題在上一個介面呼叫跳轉聊天控制器EaseMessageViewController
的時候就已經傳過來了。
2.關於暱稱跟頭像,需要修改環信的訊息擴充套件類。訊息擴充套件類在傳送各種訊息的時候都要設定。(文字、語音、表情、圖片、視訊)
最後需要用到的地方,在EaseMessageViewController.m
的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
方法的最後加上如下程式碼:
//------------------從環信伺服器獲取所有的好友 非同步載入-------------------//
[[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
if (!aError) {
self.dataArr = [[NSMutableArray alloc]initWithArray:aList];
NSLog(@"haoyou==%ld",self.dataArr.count);
//-------主執行緒重新整理UI---------
[self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
}
}];
複製程式碼
2.將獲取到的環信ID跟本地資料庫匹配,獲取中文暱稱。
//1. 利用好友賬號在資料庫中檢索,好友模型
SaveMingYiModel *model = [[SaveMingYiModel alloc]init];
self.modelArr = [[NSMutableArray alloc]init];
for (NSString *str in self.dataArr) {
model = [HaoYouDao selectedFromPeopleTableWithMDsfz:str];
if (model) {
[self.modelArr addObject:model];
}
}
//2. 通過好友模型,輸出好友名字,並新增到陣列中
NSLog(@"%ld",self.dataArr.count);
if (self.modelArr.count>0) {
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (SaveMingYiModel *model in self.modelArr) {
NSString *name = model.docName;
if (name) {
[arr addObject:name];
}
}
//3. 將漢字轉換為拼音,並進行排序
self.indexArray = [ChineseString IndexArray:arr];
self.letterResultArr = [ChineseString LetterSortArray:arr];
[self.tableView reloadData];
複製程式碼
關於按拼音首字母排序,需要匯入兩個類檔案ChineseString
、pinyin
,網上可以搜到。需要的話也可以留郵箱,我發過去。下面貼上這個介面的全部程式碼。有些是關於本地資料庫的類,關於資料庫的操作這裡不寫。
#import "HaoYouViewController.h"
#import "HaoYouTableViewCell.h"
#import "EMSDK.h"
#import "EaseUI.h"//環信相關
#import "ChineseString.h"//按照拼音首字母排序
#import "MyDoctorItem.h"//好友模型
#import "FDLoginTool.h"//登入工具類
#import "HaoYouDao.h"//資料庫DAO類
#import "SaveMingYiModel.h"//資料庫相關的好友模型
#define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
#define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
@interface HaoYouViewController ()<UITableViewDelegate,UITableViewDataSource,EMChatManagerDelegate,EMContactManagerDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) NSMutableArray *modelArr;
@property (nonatomic,strong) NSMutableArray *indexArray;
@property (nonatomic,strong) NSMutableArray *letterResultArr;
@property (nonatomic,strong) UILabel *sectionTitleView;
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation HaoYouViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"醫生列表";
self.view.backgroundColor = kGlobalBackgroundColor;
// 開啟資料庫
[HaoYouDao createYiShengTable];
// 建立表
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 65;
[self.view addSubview:_tableView];
//去掉多餘的分割線
self.tableView.tableFooterView = [[UIView alloc]init];
//改變索引字的顏色,索引背景的顏色
self.tableView.sectionIndexColor =[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
//------------------從環信伺服器獲取所有的好友 非同步載入-------------------//
[[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
if (!aError) {
self.dataArr = [[NSMutableArray alloc]initWithArray:aList];
NSLog(@"haoyou==%ld",self.dataArr.count);
//-------主執行緒重新整理UI---------
[self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
}
}];
}
/*!
@method 重新整理UI。
@abstract 載入好友列表,按首字母排序.
*/
- (void)refreshUI
{
self.sectionTitleView = ({
UILabel *sectionTitleView = [[UILabel alloc] initWithFrame:CGRectMake((APP_WIDTH-100)/2, (APP_HEIGHT-100)/2,100,100)];
sectionTitleView.textAlignment = NSTextAlignmentCenter;
sectionTitleView.font = [UIFont boldSystemFontOfSize:60];
sectionTitleView.textColor = [UIColor grayColor];
sectionTitleView.backgroundColor = [UIColor whiteColor];
sectionTitleView.layer.cornerRadius = 6;
sectionTitleView.layer.masksToBounds = YES;
sectionTitleView.layer.borderWidth = 1.f/[UIScreen mainScreen].scale;
_sectionTitleView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
sectionTitleView;
});
[self.navigationController.view addSubview:self.sectionTitleView];
self.sectionTitleView.hidden = YES;
//1. 利用好友賬號在資料庫中檢索,好友模型
SaveMingYiModel *model = [[SaveMingYiModel alloc]init];
self.modelArr = [[NSMutableArray alloc]init];
for (NSString *str in self.dataArr) {
model = [HaoYouDao selectedFromPeopleTableWithMDsfz:str];
if (model) {
[self.modelArr addObject:model];
}
}
//2. 通過好友模型,輸出好友名字,並新增到陣列中
NSLog(@"%ld",self.dataArr.count);
if (self.modelArr.count>0) {
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (SaveMingYiModel *model in self.modelArr) {
NSString *name = model.docName;
if (name) {
[arr addObject:name];
}
}
//3. 將漢字轉換為拼音,並進行排序
self.indexArray = [ChineseString IndexArray:arr];
self.letterResultArr = [ChineseString LetterSortArray:arr];
[self.tableView reloadData];
}
}
//------------------tableview 的delegate,dataSource 方法--------------//
// 返回區頭索引陣列
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexArray;
}
// 返回區頭標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [self.indexArray objectAtIndex:section];
return key;
}
// 返回區個數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.indexArray count];
}
// 返回每區的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.letterResultArr objectAtIndex:section] count];
}
// 配置單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HaoYouTableViewCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"HaoYouTableViewCell" owner:nil options:nil]lastObject];
cell.nameLabel.text = [[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
SaveMingYiModel *model = [HaoYouDao selectedFromPeopleTableWithName:[[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
//將小寫x轉換為大寫X
NSString *upStr = [model.idcardno uppercaseString];
NSLog(@"sss=%@",upStr);
//拼接頭像URL地址
NSString *imgUrl = [@"https://219.140.163.100:2265/FDAPP/app_upload/" stringByAppendingFormat:@"%@.jpg",upStr];
[cell.headImage sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
//統一設定單元格分割線,和單元格選中樣式
cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0);
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
// 設定行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.indexArray.count>0) {
return 50;
}
else
{
return kScreenH - 64;
}
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
[self showSectionTitle:title];
return index;
}
#pragma mark - private
- (void)timerHandler:(NSTimer *)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:.3 animations:^{
self.sectionTitleView.alpha = 0;
} completion:^(BOOL finished) {
self.sectionTitleView.hidden = YES;
}];
});
}
-(void)showSectionTitle:(NSString*)title{
[self.sectionTitleView setText:title];
self.sectionTitleView.hidden = NO;
self.sectionTitleView.alpha = 1;
[self.timer invalidate];
self.timer = nil;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerHandler:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
#pragma mark - UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 25)];
headerView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 40, 25)];
lab.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
lab.font = [UIFont systemFontOfSize:12];
lab.text = [self.indexArray objectAtIndex:section];
lab.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
[headerView addSubview:lab];
return headerView;
}
// 點選好友,進入詳情介面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 找到好友模型
SaveMingYiModel *model = [HaoYouDao selectedFromPeopleTableWithName:[[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
// 根據好友的身份證MD5 跳轉聊天介面
EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:model.sfzMD5 conversationType:EMConversationTypeChat];
// 隱藏底部tabBar
chatController.hidesBottomBarWhenPushed = YES;
// 聊天標題
chatController.title = model.docName;
[self.navigationController pushViewController:chatController animated:YES];
}
// 設定區頭區尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 25;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.title = @"醫生列表";
}
複製程式碼
至此,聊天功能大部分寫完,下面貼一張本地資料庫的圖。
環信ID要的是數字跟字母,所以在專案中,我就用身份證MD5加密作為環信的ID,資料庫中每條好友資訊都是從伺服器中請求的,存入資料庫,方便通過mdSfz(也就是通過環信請求到的好友ID)這個欄位去資料庫檢索資訊。 ###最後說一下訊息推送部分 1.遠端推送。之前在AppDelegate中的設定如果沒有什麼問題,並且推送證書 p12檔案都是正確的話,(p12檔案要上傳到環信後臺)遠端推送就沒有任何問題。2.本地推送。在MainViewController.m
中處理,(進入程式的第一個檢視控制器)首先匯入標頭檔案EaseSDKHelper.h
在viewDidLoad
中註冊聊天模組監聽
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
然後寫入如下程式碼:
/*!
@method 環信方法,監聽收到訊息
@abstract 不管app前臺、後臺,收到訊息都會來到這個方法
*/
- (void)messagesDidReceive:(NSArray *)aMessages
{
// 前臺就不跳轉
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
// 震動
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
EMMessage *message = aMessages[0];
//獲得訊息體
EMMessageBody *messageBody = message.body;
NSString *str;
if (messageBody.type==EMMessageBodyTypeText) {
//訊息為文字時,強轉為EMTextMessageBody 獲取文字訊息
EMTextMessageBody *textBody =(EMTextMessageBody *)messageBody;
str = textBody.text;
}
//判斷訊息型別,輸出相應型別
else if (messageBody.type==EMMessageBodyTypeImage)
{
str = @"[圖片]";
}
else if (messageBody.type==EMMessageBodyTypeVoice)
{
str = @"[語音]";
}
else if (messageBody.type==EMMessageBodyTypeLocation)
{
str = @"[位置]";
}
else if (messageBody.type==EMMessageBodyTypeVideo)
{
str = @"[視訊]";
}
else if (messageBody.type==EMMessageBodyTypeFile)
{
str = @"[檔案]";
}
else
{
str = @"";
}
//message.ext[@"img"];//頭像
//message.ext[@"accountName"];//暱稱
NSString *notifyMessage = [NSString stringWithFormat:@"%@:%@",message.ext[@"accountName"],str];
NSLog(@"notifyMessage%@",notifyMessage);
UILocalNotification *notify = [[UILocalNotification alloc]init];
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
notify.fireDate = fireDate;
//時區
notify.timeZone = [NSTimeZone defaultTimeZone];
//通知內容
notify.alertBody = notifyMessage;
//通知被觸發時播放的聲音
notify.soundName = UILocalNotificationDefaultSoundName;
//通知引數
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:message.ext[@"accountName"],@"好友",@(badgeNum),@"badgeNum",nil];
notify.userInfo = dic;
// ios8後,需要新增這個註冊,才能得到授權
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 執行通知註冊
[[UIApplication sharedApplication] scheduleLocalNotification:notify];
});
}
}
複製程式碼
本地通知傳送完成,若需要本地通知相關資訊,需要在AppDelegate.m
中接收本地通知並處理。
轉載請註明出處 © XDChang