iOS環信2.0,非常簡單

一、準備工作

1、注冊環信帳號

注冊一個環信賬號之后,我們用注冊的帳號登陸。然后創建一個應用,會得到一個對應的AppKey,這個AppKey在初始化環信SDK的時候需要用到。(這個去環信官網自己弄環信)

2、制作推送證書

如果需要做離線推送的功能,需要制作一個推送證書。如果只是需要實現單聊、群聊等功能,可以跳過此步驟。個人建議剛開始接觸環信的開發者可以忽略此步驟。制作證書

3、下載環信SDK

環信SDK.下的是2.0

二、集成環信的SDK

1、把環信SDK添加到工程中

從環信官網下載下來的是一個壓縮包,解壓之后,把我們需要的環信SDK,即EaseMobSDK這個文件夾,整個的拖入到我們的工程中。如下圖:

在lib文件夾下面有兩個靜態庫,只需要用到一個,根據你的需求選擇。

libEaseMobClientSDKLite.a不包含實時語音功能,libEaseMobClientSDK.a包含所有功能。

2、添加對應的依賴庫

向Build Phases → Link Binary With Libraries 中添加依賴庫

1、MobileCoreServices.framework

2、CFNetwork.frame

3、libsqlite3.tbd

4、libstdc++.6.0.9.tbd

5、libz.tbd

6、libiconv.tbd

7、libresolv.tbd

8、libxml2.tbd

溫馨提示:注意不要添加錯了,也不能添加少了,添加完畢之后,不要著急,先編譯一下。編譯成功,則說明沒有問題;如果編譯報錯,則仔細對照上面例舉的靜態庫進行添加,直到編譯成功,再進行下一步。

3、配置工程

3.1 不包含語音靜態庫的配置方法

(1) 刪掉libEaseMobClientSDK.a,保留libEaseMobClientSDKLite.a;

(2) 在Build Settings -> Other Linker Flags 添加”fore_load”和”libEaseMobClientSDKLite.a”的相對路徑。

如下圖所示:

3.2 包含語音靜態庫的配置方法

(1) 刪掉libEaseMobClientSDKLite.a,保留libEaseMobClientSDK.a;

(2) 在Build Settings -> Other Linker Flags 添加”-ObjC”。

如下圖所示:

4、驗證SDK是否添加成功

在AppDelegate.m文件中添加環信SDK初始化的方法,記得添加頭文件”EaseMob.h”。下面提供了我用的測試AppKey,你可以替換成你自己申請的AppKey。編譯成功,則說明你已經正確集成了環信的SDK了。

如果編譯有問題,可能存在的原因:

(1) 靜態庫沒有添加正確;

(2) 靜態庫工程配置不正確


三、添加UI文件到你的工程

集成環信2.0UI文件,需要添加的文件,如下圖所示:

添加完成之后,如下圖所示:

四、設置pch文件的路徑

文件添加成功之后,編譯會報錯,因為你沒有添加pch文件。自己手動添加pch文件(EaseUI-Prefix.pch),設置一下pch文件的加載路徑即可。如下圖所示:

在EaseUI-Prefix.pch中添加頭文件”EaseUI.h”,如下圖:

最后,編譯一下,編譯成功則說明添加集成UI文件成功。

五,搭建基本框架

1、新建三個UIViewController

新建三個ViewController,繼承UIViewController,分別命名為:FirstViewController,SecondViewController,ThirdViewController。如下圖所示

2、添加登陸方法

在AppDelegate.m中添加如下代碼:

#define APPKEY? ? ? @"1101#testrongyun"? ? //環信APPKEY

#define APNSCert? ? @"TestHuanXin"? ? ? ? ? //環信推送證書名稱

#import "AppDelegate.h"

#import "EaseMob.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "ThirdViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

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

// Override point for customization after application launch.

//初始化環信SDK

[[EaseMob sharedInstance] registerSDKWithAppKey:APPKEY apnsCertName:APNSCert];

//異步登陸的方法(這里的賬號密碼要去環信后臺自己注冊)

[[EaseMob sharedInstance].chatManager asyncLoginWithUsername:@"賬號" password:@"密碼" completion:^(NSDictionary *loginInfo, EMError *error) {

if (!error && loginInfo) {

NSLog(@"登陸成功");

[self setUpNav];

}

} onQueue:nil];

return YES;

}

- (void)setUpNav

{

FirstViewController *firstVC = [[FirstViewController alloc] init];

SecondViewController *secondVC = [[SecondViewController alloc] init];

ThirdViewController *thirdVC = [[ThirdViewController alloc] init];

firstVC.title = @"會話列表";

secondVC.title = @"通訊錄";

thirdVC.title = @"設置";

UITabBarController *tabBar = [[UITabBarController alloc] init];

tabBar.viewControllers = @[[[UINavigationController alloc] initWithRootViewController:firstVC],

[[UINavigationController alloc] initWithRootViewController:secondVC],

[[UINavigationController alloc] initWithRootViewController:thirdVC]];

self.window.rootViewController = tabBar;

self.window.backgroundColor = [UIColor whiteColor];

}

@end

編譯一下,看下效果。

六、添加與聊天有關的文件

1、添加GifImage文件2、添加chat文件

添加完成之后,編譯一下,把報錯的地方全部注釋掉,有很多地方需要注釋掉,這些地方是因為有些我們不需要的文件沒有添加進來。(自己注釋比較麻煩)

注釋好的GifImage和chat文件,下載后無需注釋無關代碼,可直接使用注釋好的文件,

八、實現單聊

在SecondViewController.m中添加如下代碼:

#import "SecondViewController.h"#import "ChatViewController.h"@interface SecondViewController (){

NSArray *arrSystem;

NSArray *arrFriends;

}

@property (retain, nonatomic)? UITableView *tableView;

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

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

_tableView = [[UITableView alloc] initWithFrame:self.view.frame];

_tableView.delegate = self;

_tableView.dataSource = self;

[self.view addSubview:_tableView];

//獲取好友列表

[[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {

if (!error) {

NSLog(@"獲取成功 -- %@",buddyList);

arrFriends = [NSArray arrayWithArray:buddyList];

[_tableView reloadData];

}

} onQueue:nil];

}

#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;

}

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

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

ChatViewController *chatVC = [[ChatViewController alloc] initWithConversationChatter:buddy.username conversationType:eConversationTypeChat];

chatVC.title = buddy.username; //好友的名字

chatVC.hidesBottomBarWhenPushed = YES;

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

}

編譯,效果

真機運行一下,可能會報錯,

解決方案:

把這個值設置成no
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、準備工作 1、注冊環信帳號注冊一個環信賬號之后,我們用注冊的帳號登陸。然后創建一個應用,會得到一個對應的App...
    niceSYT閱讀 419評論 1 2
  • 第一次集成環信SDK應該是在去年吧,那個時候還是2.0的版本,沒想到后面做的幾個APP都用到了環信的及時聊天,用過...
    imGeek閱讀 1,634評論 4 16
  • 上官網注冊賬號 首先來到環信的官網,然后登陸.沒有賬號先注冊一個. 進去之后創建應用,如圖 創建應用界面 點擊確定...
    loneWolf01閱讀 522評論 0 0
  • 六.集成SDK基礎功能 (仔細研習,結合實際操作) 由于環信官方文檔比較詳細,因此,根據項目需求,把用到的方法記錄...
    夜空已沉寂閱讀 2,160評論 0 7
  • (十一)倩倩的變節 在烈士陵園待了一會,實在覺得無聊,常毛建議我們去看錄像。很久都沒有過錄像癮了,我們在對此提議的...
    兒和女閱讀 215評論 0 0