最新公司的項(xiàng)目比較趕躁倒,國(guó)慶期間也在趕項(xiàng)目。很久沒(méi)有發(fā)表新的動(dòng)態(tài)了轴或。由于公司要求一定要使用阿里百川的即時(shí)通訊越平,一起分享下經(jīng)驗(yàn)频蛔。下周再分享環(huán)信SDK
一.即時(shí)通訊簡(jiǎn)介
1.1 XMPP簡(jiǎn)介
-
XMPP
一個(gè)即時(shí)通訊的協(xié)議,它規(guī)范了用于即時(shí)通信在網(wǎng)絡(luò)上數(shù)據(jù)傳輸格式的秦叛,比如登錄晦溪,獲取好友列表等等的格式,XMPP
在網(wǎng)絡(luò)傳輸?shù)臄?shù)據(jù)是XML格式 -
XMPP
是一個(gè)基于Socket
通過(guò)的網(wǎng)絡(luò)協(xié)議挣跋,目的就是為了保護(hù)長(zhǎng)連接三圆,以實(shí)現(xiàn)即時(shí)通訊功能 -
XMPP
的客戶(hù)端是使用一個(gè)XMPPFramework
框架實(shí)現(xiàn) -
XMPP
的服務(wù)器是使用Openfire
,一個(gè)開(kāi)源的服務(wù)器
1.2 第三方即時(shí)通訊SDK
- 不需要公司內(nèi)部搭建服務(wù)器
- 可使公司可以節(jié)約時(shí)間成本
- 客戶(hù)端的開(kāi)發(fā)避咆,使用第三方SDK比XMPPFramework更簡(jiǎn)潔方便
- 是在
XMPP
的基礎(chǔ)上進(jìn)行二次開(kāi)發(fā)舟肉,對(duì)服務(wù)器Openfire
和客戶(hù)端進(jìn)行功能模型的添加和客戶(hù)端SDK的封裝,本質(zhì)還是使用XMPP
,基于Socket的網(wǎng)絡(luò)通信 - 內(nèi)部實(shí)現(xiàn)了數(shù)據(jù)緩存查库,會(huì)把聊天記錄添加到數(shù)據(jù)庫(kù)路媚,把附件(如音頻文件,圖片文件)下載到本地樊销,使程序員更多時(shí)間是花到用戶(hù)即時(shí)體驗(yàn)上
只要你使用第三方的即時(shí)通訊,無(wú)論你是使用環(huán)信還是融云還是阿里的,首先第一步就是集成整慎,集成千篇一律适荣,對(duì)著官方文檔完成集成即可,集成問(wèn)題則不再細(xì)說(shuō)
二.阿里百川SDK準(zhǔn)備工作
2.1 注冊(cè)賬號(hào)
2.2 根據(jù)build id創(chuàng)建應(yīng)用,集成的時(shí)候需要替換阿里AppKey
值
三. 集成與核心功能(主要針對(duì)單聊)
2 查看示例demo并熟悉阿里百川的示例demo下載
3 讓示例demo變成你的東西,根據(jù)項(xiàng)目需求院领,對(duì)其進(jìn)行增刪改減。把
SPKitExample
類(lèi)名改成SPKitManager
這樣意思更明確够吩,再者把SPKitExample
類(lèi)里面所有的example
全部清除
整理好之后剩下如下文件
四. 步驟具體化
1.初始化IMSDK
在AppDelegate
中導(dǎo)入#import "SPKitManager.h"
頭文件比然,并在didFinishLaunchingWithOptions
方法中初始化IMSDK
[[SPKitManager sharedInstance] callThisInDidFinishLaunching];
2.設(shè)置SPKitManager
首先在SPKitManager.h
添加一個(gè)block塊來(lái)檢查登錄狀態(tài)
-(void)checkLoginRun:(void(^)())callback;
其次在SPKitManager.m
聲明一個(gè)BOOL屬性判斷用戶(hù)是否登錄,并實(shí)現(xiàn)checkLoginRun
方法,為了保護(hù)用戶(hù)賬號(hào)密碼的安全性周循,我們對(duì)用戶(hù)的賬號(hào)密碼進(jìn)行MD5加密處理强法。XXXX
一般是以項(xiàng)目名稱(chēng)做其前綴進(jìn)行加密,不過(guò)具體還是得看公司要求湾笛。
@property(nonatomic)BOOL isLogined;
-(void)checkLoginRun:(void(^)())callback{
[self login:callback];
}
-(void)login:(void(^)())callback{
if(self.isLogined){
if (callback) {
callback();
}
}else{
@synchronized(self){
if(self.isLogined) {
if (callback) {
callback();
}
return;
}
YISUserModel<Optional>* user = [YISUserManager shareInstance].user.user;
if(user == nil || user.id == nil){return;}
NSString *uid = [YISTools MD5String: user.id];
NSString *pwd = [YISTools MD5String:[NSString stringWithFormat:@"XXXX%@",user.id]];
[self callThisAfterISVAccountLoginSuccessWithYWLoginId:uid passWord:pwd preloginedBlock:^{}
successBlock:^{
self.isLogined = YES;
if (callback) {
callback();
} } failedBlock:^(NSError *aError) {
DDLogInfo(@"aError:%@",aError);
}]; }
}
}
3.創(chuàng)建會(huì)話(huà)列表
- 3.1導(dǎo)入頭文件3個(gè)
#import <WXOUIModule/YWConversationListViewController.h>
#import "SPKitManager.h"
#import <WXOpenIMSDKFMWK/YWConversation.h>
- 3.2聲明屬性
@property(nonatomic, strong) SPKitManager* spkit;//聊天Manager
@property (weak, nonatomic) IBOutlet UIView *msgListView;
@implementation YISMessageViewController{
NSString *currentId;
YWConversationListViewController *conversationListController;
}
- 3.3設(shè)置會(huì)話(huà)列表
viewDidLoad
->調(diào)用[self setupIMList]
(判斷用戶(hù)是否登錄以及是否切換賬號(hào)等問(wèn)題的處理)->[self _setupIMList]
(設(shè)置會(huì)話(huà)列表)
-(void)setupIMList{
BOOL isLogin = [YISUserManager shareInstance].isLogin;
if (!isLogin) {
[[NSNotificationCenter defaultCenter] postNotificationName:kLoginNotification object:nil];
return;
}
NSString *uid = [YISUserManager shareInstance].user.user.id;
if(currentId != nil && ![currentId isEqualToString: uid]){
[self.msgListView removeFromSuperview];
[conversationListController removeFromParentViewController];
DDLogDebug(@"currentId%@--userid:%@",currentId,uid);
}else if (currentId != nil){
return;
}
self.spkit = [SPKitManager sharedInstance];
[self.spkit checkLoginRun:^{
[self _setupIMList];
}];
}
-(void)_setupIMList{
conversationListController = [self.spkit.ywIMKit makeConversationListViewController];
conversationListController.view.frame = [UIScreen mainScreen].bounds;
conversationListController.view.backgroundColor =[UIColor clearColor];
__weak __typeof(conversationListController) weakConversationListController;
weakConversationListController = conversationListController;
YWConversationsListDidSelectItemBlock selectItemBlock;
selectItemBlock = ^(YWConversation *aConversation) {
if ([aConversation isKindOfClass:[YWCustomConversation class]]) {
YWCustomConversation *customConversation = (YWCustomConversation *)aConversation;
[customConversation markConversationAsRead];
}
else {
[[SPKitManager sharedInstance] openConversationViewControllerWithConversation:aConversation
fromNavigationController:weakConversationListController.navigationController];
} };
[conversationListController setDidSelectItemBlock:selectItemBlock];
[self.spkit CustomizeConversationCellWithConversationListController:conversationListController];
conversationListController.didDeleteItemBlock = ^ (YWConversation *aConversation) {};
self.msgListView = conversationListController.view;
[self.view addSubview:self.msgListView];
[self addChildViewController:conversationListController];
[self didMoveToParentViewController:conversationListController];
__weak typeof(self.navigationController) weakController = self.navigationController;
[self.spkit.ywIMKit setUnreadCountChangedBlock:^(NSInteger aCount) {
NSString *badgeValue = aCount > 0 ?[ @(aCount) stringValue] : nil;
weakController.tabBarItem.badgeValue = badgeValue;
}];
}
4.退出登錄
上面有有提到賬號(hào)切換問(wèn)題饮怯,而在app退出登錄時(shí)阿里也要退出,否則會(huì)導(dǎo)致此種情況嚎研,登錄新的賬號(hào)時(shí)還殘留著上一個(gè)賬號(hào)的聊天列表蓖墅。如果程序每次卸載再重新安裝不會(huì)發(fā)現(xiàn)此問(wèn)題,但是沒(méi)有必要這樣临扮,用戶(hù)體驗(yàn)會(huì)及其不好论矾。
例如:
[[YISUserManager shareInstance]logout];
[[SPKitManager sharedInstance]Logout];
額外補(bǔ)充
替換阿里的默認(rèn)頭像
WXFrameworks->WXOUIModuleResources.bundle->pub_ico_single_120@2x/pub_ico_single_120@3x