IOS [ 即時(shí)通訊 ]發(fā)送文本消息 代碼

????```

import "bingeChatViewController.h"

import "bingeInputView.h"

/*
1.隱藏和顯示tabBar
2.為inputView 加分割線
3.title 好友聊天標(biāo)題
4.監(jiān)聽(tīng)鍵盤(pán)彈出 對(duì)相應(yīng)的布局做修改
5.獲取發(fā)送消息信息
*/

// 宏 自定義View高度為44

define KInputViewH 44

@interface bingeChatViewController () <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate>

//由這兩個(gè)控件組成
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) bingeInputView *inputView;

// 發(fā)送消息列表數(shù)組
@property (nonatomic,strong) NSMutableArray *chatMsgs;

@end

@implementation bingeChatViewController
// 1.
-(UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]init];
// 代理方法
_tableView.dataSource = self;
_tableView.delegate = self;
// Xib View 的位置
_tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - KInputViewH);
}
return _tableView;
}

// 2.
-(UIView *)inputView {
if (!_inputView) {
_inputView = [bingeInputView binge_inputView];
_inputView.textField.delegate = self;
_inputView.frame = CGRectMake(0, self.view.bounds.size.height - KInputViewH, self.view.bounds.size.width, KInputViewH);
}
return _inputView;
}

  • (NSMutableArray *)chatMsgs {
    if (!_chatMsgs) {
    _chatMsgs = [NSMutableArray array];
    }
    return _chatMsgs;
    }

// 3.

  • (void)viewDidLoad {
    [super viewDidLoad];
    // 添加子控件
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.inputView];
    // 添加標(biāo)題
    self.title = self.buddy.username;
// 點(diǎn)擊空白處收鍵盤(pán)
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];

[self.view addGestureRecognizer:singleTap];

// 獲取會(huì)話中的聊天記錄
[self binge_reloadChatMsgs];

//監(jiān)聽(tīng)鍵盤(pán)彈出 對(duì)相應(yīng)的布局組修改
[self binge_observerKeyboardFrameChange];

}

pragma mark - tabBar 隱藏和顯示

  • (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 隱藏 tabBar 控件
    self.navigationController.tabBarController.tabBar.hidden = YES;
    }
  • (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 隱藏 tabBar 控件
    self.navigationController.tabBarController.tabBar.hidden = NO;
    }

pragma mark - UITableViewDataSource

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.chatMsgs.count;
    }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chatID"];
if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"chatID"];
    EMMessage *msg = self.chatMsgs[indexPath.row];
    NSLog(@"msg = %@",msg);
    EMTextMessageBody *body = msg.messageBodies.firstObject;
    cell.textLabel.text = body.text;
}
return cell;  // 創(chuàng)建消息后 記得返回到cell

}

pragma mark - 收起鍵盤(pán)

// 滑動(dòng)空白處隱藏鍵盤(pán)

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    [self.view endEditing:YES];
    }

// 點(diǎn)擊空白處收鍵盤(pán)
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer {

[self.view endEditing:YES];

}

pragma mark - UITextFieldDelegate 發(fā)送消息

  • (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 此處發(fā)送消息
    // 3.創(chuàng)建EMChatText 對(duì)象
    EMChatText *chatText = [[EMChatText alloc] initWithText:textField.text];

    // 2.創(chuàng)建body 對(duì)象,發(fā)現(xiàn)需要chatText
    EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithChatObject:chatText];

    // 1.創(chuàng)建消息,發(fā)現(xiàn)body 對(duì)象
    EMMessage *emsg = [[EMMessage alloc] initWithReceiver:self.buddy.username bodies:@[body]];

    // 0.要發(fā)送消息,發(fā)現(xiàn)需要?jiǎng)?chuàng)建消息
    // 異步方法,發(fā)送一條消息
    // 待發(fā)送的消息對(duì)象和發(fā)送后的消息對(duì)象是同一個(gè)對(duì)象,在發(fā)送過(guò)程中對(duì)象屬性可能會(huì)被更改
    [[EaseMob sharedInstance].chatManager asyncSendMessage:emsg
    progress:nil
    prepare:^(EMMessage *message, EMError *error) {
    // 即將發(fā)送時(shí)間的回調(diào)
    }
    onQueue:nil
    completion:^(EMMessage *message, EMError *error) {
    // 發(fā)送結(jié)束時(shí)的回調(diào)
    if (!error) {
    NSLog(@"發(fā)送成功");
    // 清空文本輸入款
    textField.text = nil;
    // 發(fā)送成功之后將鍵盤(pán)退回去
    [textField resignFirstResponder];
    // 刷新界面
    [self binge_reloadChatMsgs];
    }
    }
    onQueue:nil];

// 發(fā)送成功之后 刷新列表  【定義一個(gè)可變數(shù)組】

return YES;

}

pragma mark - 私有方法

  • (void)binge_reloadChatMsgs {
    // 首先刷新的時(shí)候要移除已有的對(duì)象
    [self.chatMsgs removeAllObjects];

    // 拿到當(dāng)前的會(huì)話對(duì)象 一個(gè)傳username 另一個(gè)傳聊天樣式
    /*
    @brief 會(huì)話類型
    @constant eConversationTypeChat 單聊會(huì)話
    @constant eConversationTypeGroupChat 群聊會(huì)話
    @constant eConversationTypeChatRoom 聊天室會(huì)話
    */
    EMConversation *conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:self.buddy.username conversationType:eConversationTypeChat];
    // 獲取會(huì)話中的聊天記錄 (從數(shù)據(jù)庫(kù)中加載消息)
    NSArray *msgs = [conversation loadAllMessages];
    // 放進(jìn)可變數(shù)組中 改變數(shù)據(jù)
    [self.chatMsgs addObjectsFromArray:msgs];

    // 刷新表格
    [self.tableView reloadData];
    // 滾到最下面

}

  • (void)binge_observerKeyboardFrameChange {

    // 監(jiān)聽(tīng)鍵盤(pán) 對(duì)相應(yīng)的布局做修改
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillChangeFrameNotification
    object:nil
    queue:[NSOperationQueue mainQueue]// 在哪個(gè)線程執(zhí)行
    usingBlock:^(NSNotification * _Nonnull note) {
    NSLog(@"%s,line = %d,note = %@",FUNCTION,LINE,note);

                                                    /*
                                                     note.userInfo 布局參數(shù)
                                                     UIKeyboardAnimationCurveUserInfoKey = 7;
                                                     UIKeyboardAnimationDurationUserInfoKey = "0.25";
                                                     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";
                                                     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 600.5}";
                                                     UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 871.5}";
                                                     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";
                                                     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";
                                                     UIKeyboardIsLocalUserInfoKey = 1;
                                                     
                                                     self.view 可以根據(jù) end.oriY 來(lái)進(jìn)行布局更改
                                                     
                                                     */
                                                    // 初始高度
                                                    CGFloat endY = [note.userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
                                                    // 觸發(fā)的鍵盤(pán)高度
                                                    CGFloat tempY = endY - self.view.bounds.size.height;
                                                    self.view.frame = CGRectMake(0, tempY, self.view.bounds.size.width, self.view.bounds.size.height);
                                                    
                                                    // 添加動(dòng)畫(huà)
                                                    CGFloat duration = [note.userInfo [UIKeyboardAnimationDurationUserInfoKey] floatValue];
                                                    [UIView animateWithDuration:duration animations:^{
                                                        [self.view setNeedsLayout];
                                                    }];
                                                    
                                                }];
    

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市汗捡,隨后出現(xiàn)的幾起案子圈匆,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡祖秒,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)剂陡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)狈涮,“玉大人狐胎,你說(shuō)我怎么就攤上這事鸭栖。” “怎么了握巢?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵晕鹊,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我暴浦,道長(zhǎng)溅话,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任歌焦,我火速辦了婚禮飞几,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘独撇。我一直安慰自己屑墨,他們只是感情好躁锁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著卵史,像睡著了一般战转。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上以躯,一...
    開(kāi)封第一講書(shū)人閱讀 51,165評(píng)論 1 299
  • 那天槐秧,我揣著相機(jī)與錄音,去河邊找鬼忧设。 笑死刁标,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的见转。 我是一名探鬼主播命雀,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼斩箫!你這毒婦竟也來(lái)了吏砂?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤乘客,失蹤者是張志新(化名)和其女友劉穎狐血,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體易核,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡匈织,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了牡直。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缀匕。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖碰逸,靈堂內(nèi)的尸體忽然破棺而出乡小,到底是詐尸還是另有隱情,我是刑警寧澤饵史,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布满钟,位于F島的核電站,受9級(jí)特大地震影響胳喷,放射性物質(zhì)發(fā)生泄漏湃番。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一吭露、第九天 我趴在偏房一處隱蔽的房頂上張望吠撮。 院中可真熱鬧,春花似錦讲竿、人聲如沸泥兰。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)逾条。三九已至琢岩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間师脂,已是汗流浹背担孔。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留吃警,地道東北人糕篇。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像酌心,于是被迫代替她去往敵國(guó)和親拌消。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容