一弄屡、準(zhǔn)備工作
1、注冊環(huán)信帳號注冊一個(gè)環(huán)信賬號之后鞋诗,我們用注冊的帳號登陸琢岩。然后創(chuàng)建一個(gè)應(yīng)用,會(huì)得到一個(gè)對應(yīng)的AppKey,這個(gè)AppKey在初始化環(huán)信SDK的時(shí)候需要用到师脂。(這個(gè)去環(huán)信官網(wǎng)自己弄環(huán)信)
2担孔、制作推送證書如果需要做離線推送的功能,需要制作一個(gè)推送證書吃警。如果只是需要實(shí)現(xiàn)單聊糕篇、群聊等功能,可以跳過此步驟酌心。個(gè)人建議剛開始接觸環(huán)信的開發(fā)者可以忽略此步驟拌消。制作證書
3、下載環(huán)信sdk?.下的是2.0
二安券、集成環(huán)信的SDK
1墩崩、把環(huán)信SDK添加到工程中
從環(huán)信官網(wǎng)下載下來的是一個(gè)壓縮包,解壓之后侯勉,把我們需要的環(huán)信SDK鹦筹,即EaseMobSDK這個(gè)文件夾,整個(gè)的拖入到我們的工程中址貌。如下圖:
在lib文件夾下面有兩個(gè)靜態(tài)庫铐拐,只需要用到一個(gè)徘键,根據(jù)你的需求選擇。
libEaseMobClientSDKLite.a不包含實(shí)時(shí)語音功能遍蟋,libEaseMobClientSDK.a包含所有功能吹害。
2、添加對應(yīng)的依賴庫
向Build Phases → Link Binary With Libraries 中添加依賴庫
MobileCoreServices.framework
CFNetwork.frame
libsqlite3.tbd
libstdc++.6.0.9.tbd
libz.tbd
libiconv.tbd
libresolv.tbd
libxml2.tbd
溫馨提示:注意不要添加錯(cuò)了虚青,也不能添加少了它呀,添加完畢之后,不要著急棒厘,先編譯一下钟些。編譯成功,則說明沒有問題绊谭;如果編譯報(bào)錯(cuò)政恍,則仔細(xì)對照上面例舉的靜態(tài)庫進(jìn)行添加,直到編譯成功达传,再進(jìn)行下一步篙耗。
3、配置工程
3.1 不包含語音靜態(tài)庫的配置方法
(1) 刪掉libEaseMobClientSDK.a宪赶,保留libEaseMobClientSDKLite.a宗弯;
(2) 在Build Settings -> Other Linker Flags 添加”fore_load”和”libEaseMobClientSDKLite.a”的相對路徑。
如下圖所示:
3.2 包含語音靜態(tài)庫的配置方法
(1) 刪掉libEaseMobClientSDKLite.a搂妻,保留libEaseMobClientSDK.a蒙保;
(2) 在Build Settings -> Other Linker Flags 添加”-ObjC”。
如下圖所示:
4欲主、驗(yàn)證SDK是否添加成功
在AppDelegate.m文件中添加環(huán)信SDK初始化的方法邓厕,記得添加頭文件”EaseMob.h”。下面提供了我用的測試AppKey扁瓢,你可以替換成你自己申請的AppKey详恼。編譯成功,則說明你已經(jīng)正確集成了環(huán)信的SDK了引几。
如果編譯有問題昧互,可能存在的原因:
(1) 靜態(tài)庫沒有添加正確;
(2) 靜態(tài)庫工程配置不正確
#define APPKEY? ? ? @"1101#testrongyun"? ? //環(huán)信APPKEY
#define APNSCert? ? @"TestHuanXin"? ? ? ? ? //環(huán)信推送證書名稱
#import "AppDelegate.h"
#import "EaseMob.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//初始化環(huán)信SDK[[EaseMob sharedInstance] registerSDKWithAppKey:APPKEY apnsCertName:APNSCert];
return YES;
}
三伟桅、添加UI文件到你的工程
集成環(huán)信2.0UI文件敞掘,需要添加的文件,如下圖所示:
添加完成之后楣铁,如下圖所示:
四玖雁、設(shè)置pch文件的路徑
文件添加成功之后,編譯會(huì)報(bào)錯(cuò)民褂,因?yàn)槟銢]有添加pch文件茄菊。自己手動(dòng)添加pch文件(EaseUI-Prefix.pch)疯潭,設(shè)置一下pch文件的加載路徑即可赊堪。如下圖所示:
在EaseUI-Prefix.pch中添加頭文件”EaseUI.h”面殖,如下圖:
最后,編譯一下哭廉,編譯成功則說明添加集成UI文件成功脊僚。
五,搭建基本框架
1、新建三個(gè)UIViewController
新建三個(gè)ViewController,繼承UIViewController,分別命名為:FirstViewController遵绰,SecondViewController辽幌,ThirdViewController。如下圖所示
2椿访、添加登陸方法
在AppDelegate.m中添加如下代碼:
#define APPKEY? ? ? @"1101#testrongyun"? ? //環(huán)信APPKEY
#define APNSCert? ? @"TestHuanXin"? ? ? ? ? //環(huán)信推送證書名稱
#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.//初始化環(huán)信SDK
[[EaseMob sharedInstance] registerSDKWithAppKey:APPKEY apnsCertName:APNSCert];//異步登陸的方法(這里的賬號密碼要去環(huán)信后臺自己注冊)
[[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 = @"會(huì)話列表";
secondVC.title = @"通訊錄";
thirdVC.title = @"設(shè)置";
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
編譯一下,看下效果。
六彻采、添加與聊天有關(guān)的文件
1柴梆、添加GifImage文件2、添加chat文件
添加完成之后霸奕,編譯一下适揉,把報(bào)錯(cuò)的地方全部注釋掉炼邀,有很多地方需要注釋掉,這些地方是因?yàn)橛行┪覀儾恍枰奈募]有添加進(jìn)來。(自己注釋比較麻煩)
注釋好的GifImage和chat文件,下載后無需注釋無關(guān)代碼掸犬,可直接使用注釋好的文件,
七毡泻、實(shí)現(xiàn)單聊在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];
}
編譯,效果
真機(jī)運(yùn)行一下贩挣,可能會(huì)報(bào)錯(cuò),
解決方案:
把這個(gè)值設(shè)置成no
文章作者:環(huán)信熱心用戶樊呵呵