iOS 環信集成 實現單聊

一、新建一個pch文件

1、創建方法: File -> New -> File… -> Other -> PCH File,如下圖所示:

這裏寫圖片描述

2、設置Prefix Header 路徑

把pch文件往右邊的白色框框裏面拖,即可自動填充pch文件所在的路徑。

這裏寫圖片描述

3、在pch中添加宏定義和頭文件

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#define APPKEY      @"1101#testrongyun"     //環信APPKEY
#define APNSCert    @"TestHuanXin"          //環信推送證書名稱

//引入我自己框架相關的頭文件
#import "YCPublicMoudle/YCPublicMoudle.h"


//與環信SDK有關的頭文件
#import "EaseMob.h"
#import "EMError.h"

#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

需要去環信官網註冊APPKEY和申請推送證書 (點擊查看教程

二、搭建項目基本框架

1、加入項目相關的基類和相關輔助文件

這些類和文件是我平常在做項目中積累的一些東西,對一些常用的方法進行了封裝,可以提高開發效率。如果你需要可以把整個YCPublicMoudle文件拖進你的工程,如果不需要可以忽略,這些多環信的集成沒有影響。如下圖所示:

這裏寫圖片描述

2、在AppDelegate.m文件中初始化環信SDK

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //初始化環信SDK
    [[EaseMob sharedInstance] registerSDKWithAppKey:APPKEY apnsCertName:APNSCert];

    LoginViewController *loginVC = [[LoginViewController alloc] init];

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:loginVC];
    self.window.rootViewController = navigation;

    return YES;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

相關的頭文件,我都加在pch文件中了,如果你沒有創建pch文件,請添加對應的頭文件,這裏需要添加的頭文件是:

#import "EaseMob.h"
  • 1
save_snippets.png
  • 1

3、添加登陸註冊功能

環信官方文檔中註冊和登錄提供了三種接口,其實最終的目的都是一樣的,不知道同一個接口整這麼多的方式是要幹嘛?剛開始接觸時,搞得我都不知道用哪個接口,因爲選擇多了,就不知道選擇了,整一個異步方式的接口就夠了。

3.1 登陸

1、環信官方文檔登陸接口:

[[EaseMob sharedInstance].chatManager asyncLoginWithUsername:@"8001" password:@"111111" completion:^(NSDictionary *loginInfo, EMError *error) {
    if (!error && loginInfo) {
        NSLog(@"登陸成功");
    }
} onQueue:nil];
  • 1
  • 2
  • 3
  • 4
  • 5
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5

2、我工程中登陸的方法如下所示:

//登陸
- (void)btnClickLogin
{
    //異步登陸的方法
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:txtAccount.text password:txtPsw.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {
            NSLog(@"登陸成功");

        }
    } onQueue:nil];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.2 註冊

1、環信官方文檔註冊接口:

[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:@"8001" password:@"111111" withCompletion:^(NSString *username, NSString *password, EMError *error) {
    if (!error) {
        NSLog(@"註冊成功");
    }
} onQueue:nil];
  • 1
  • 2
  • 3
  • 4
  • 5
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5

2、我項目中註冊的方法如下所示:

//註冊
- (void)btnClickRegister
{
    //異步方法
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:txtAccount.text password:txtPsw.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        if (!error) {
            NSLog(@"註冊成功");

            UIAlertController *alterController= [UIAlertController alertControllerWithTitle:nil
                                                                                    message:@"註冊成功"
                                                                             preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alterController animated:YES completion:nil];

            UIAlertAction *alterAction = [UIAlertAction actionWithTitle:@"確定"
                                                                  style:UIAlertActionStyleCancel
                                                                handler:nil];
            [alterController addAction:alterAction];

        }
    } onQueue:nil];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4. 登陸註冊的效果圖

1、APP效果圖

這裏寫圖片描述這裏寫圖片描述

2、環信後臺效果圖

登陸到環信後臺,你會發現在後臺管理中,可以看到你剛剛註冊的用戶了,有木有感覺很神奇。小夥子不錯,看好你喔,繼續加油。

這裏寫圖片描述

三、集成環信單聊UI

小編在這個過程中,跳了很多坑。你如果直接看官方demo,你會瘋了去,因爲他所有的代碼都攪在一起了,一時半會兒感覺無從下手。環信還算比較人性化,錄製了一個集成UI的視頻,可是照着搞了半天,老是各種頭文件缺失,各種報錯。最後去問技術支持,他只是弱弱的說了一句,視頻教程不是最新的了,小編頓時感覺一萬隻草泥馬在奔騰。

3.1 添加與單聊相關的文件

從官方下載的Demo中把這幾個文件揪出來,然後添加到你的工程中。當你迫不及待的去編譯你的工程時,你會發現報錯了。不要懷疑自己,你沒錯,報錯是正常的。Demo裏面的設計很不好,沒有做到模塊化,有一些不需要的東西,都弄進來了,所以你要把報錯的文件全部註釋掉,估計有好幾十個吧。小夥子,要有耐心,看好你喔。

3rdparty,
Category,
Custom,
Resources,
Class ->Chat -> ChatView
ChatDemoUIDefine.h

如你覺得太麻煩了,那就直接下載小編已經爲你註釋好了的文件吧,下載之後直接加入你的工程即可。(點擊下載集成UI文件

然後,運行你的工程,結果報錯了。不好意識,小編忘了,還要在 pch 文件中加入對應的頭文件:

#import "UIViewController+HUD.h"
#import "ChatDemoUIDefine.h"
#import "EMAlertView.h"
  • 1
  • 2
  • 3
save_snippets.png
  • 1
  • 2
  • 3

編譯一下,這次應該沒有問題了吧。

3.2 新建一個TabBarViewController

1、創建一個TabBarViewController,然後在分別創建會話列表、通訊錄列表、設置列表,這幾個類都是繼承UITableViewControler,如下圖所示:

這裏寫圖片描述

2、在YCTabBarViewController中添加如下代碼:

#import "YCTabBarViewController.h"
#import "ChatListViewController.h"
#import "AddressBookViewController.h"
#import "SettingsViewController.h"

@interface YCTabBarViewController ()

@end

@implementation YCTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    ChatListViewController *chatListVC = [[ChatListViewController alloc] init];
    chatListVC.tabBarItem.title = @"會話";
    chatListVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_chatsHL"];

    AddressBookViewController *addressBookVC = [[AddressBookViewController alloc] init];
    addressBookVC.tabBarItem.title = @"通訊錄";
    addressBookVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_contactsHL"];

    SettingsViewController *settingVC = [[SettingsViewController alloc] init];
    settingVC.tabBarItem.title = @"設置";
    settingVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_settingHL"];

    self.viewControllers = @[chatListVC,addressBookVC,settingVC];
}

@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3、然後把以上幾個類與登陸按鈕關聯起來,點擊登陸按鈕跳轉到YCTabBarViewController中來。在LoginViewController類中的登陸方法中,添加如下代碼,記得加入頭文件 YCTabBarViewController.h 。

//登陸
- (void)btnClickLogin
{
    //異步登陸的方法
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:txtAccount.text password:txtPsw.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {

            YCTabBarViewController *tabBarVC = [[YCTabBarViewController alloc] init];
            [self.navigationController pushViewController:tabBarVC animated:YES];
            NSLog(@"登陸成功");

        }
    } onQueue:nil];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4、好了,我們編譯一下,來看下效果如何。

這裏寫圖片描述

3.3 獲取好友列表

要想實現單聊,那麼首先應該要有一個聊天的對象,是吧,自己和自己聊天好像不太合適吧,我們不需要自我溝通,那麼我們就先要獲取好友列表。

1、進入環信後臺添加你的好友

首先,你需要通過APP的註冊功能註冊一些測試的賬號,然後在環信的後臺添加一些好友,如下圖:

這裏寫圖片描述

2、在APP中實現加載好友列表

環信加載好友列表的接口爲:

// 獲取好友列表
[[EaseMob sharedInstance].chatManager asyncFetchBuddyList];
  • 1
  • 2
save_snippets.png
  • 1
  • 2

然後,在AddressBookViewController類中添加如下代碼:

#import "AddressBookViewController.h"

@interface AddressBookViewController ()
{
    NSArray *arrSystem;
    NSArray *arrFriends;
}

@end

@implementation AddressBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    arrSystem = @[@"申請與通知",@"羣聊",@"聊天室"];

    arrFriends = [[EaseMob sharedInstance].chatManager buddyList]; //獲取好友列表
}


#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

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

    if (section == 0) {
        return arrSystem.count;
    } else {
        return arrFriends.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    switch (indexPath.section) {
        case 0:
        {
            cell.textLabel.text = [arrSystem objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:@"groupPublicHeader"];
            break;
        }

        case 1:
        {
            EMBuddy *eMBuddy = [arrFriends objectAtIndex:indexPath.row];
            cell.textLabel.text = eMBuddy.username;
            cell.imageView.image = [UIImage imageNamed:@"chatListCellHead"];
            break;
        }

        default:
            break;
    }

    return cell;
}


@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

好吧,我知道你等不及了,我們先編譯一下,來看看效果吧。

這裏寫圖片描述

上面那三個是後面實現其他功能需要的,下面那三個纔是好友喔,不知道你跟上我的節奏了麼。哇塞,好緊張喔,下面終於要開始實現單聊了。

3.4 實現單聊

其實,代碼寫到這裏已經萬事具備了,真的就只欠一個列表點擊事件了。在AddressBookViewController類中添加一個點擊事件,如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    EMBuddy *buddy = [arrFriends objectAtIndex:indexPath.row];

    ChatViewController *chatVC = [[ChatViewController alloc] initWithChatter:buddy.username isGroup:NO];
    chatVC.title = buddy.username; //好友的名字

    [self.navigationController pushViewController:chatVC animated:YES];

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
save_snippets.png
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

特別說明一下,EMBuddy是一個好友信息的描述類。

我的工程裏面有一個 ChatViewController類,這個類已經集成聊天的所有東西,這個是我們之前集成環信單聊UI裏面拖進來的一個文件。裏面集成了所有與聊天的功能,我們只需要用就OK了。

這裏寫圖片描述

好吧,激動人心的時刻終於來臨了,我們來看下最終的結果。
這裏寫圖片描述
這裏寫圖片描述

跟着思路來,其實還是蠻簡單的麼。哎呀,一不小心已經到中午了,麻麻喊我去恰飯噠,如何在APP上實現添加好友,下次在告訴你。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章