云通訊簡介
騰訊是國內最大也是最早的即時通信開發商,QQ和微信已經成為每個互聯網用戶必不可少的應用。現在,騰訊將高并發、高可靠的即時通信能力進行開放,開發者可以很容易的根據騰訊提供的SDK將即時通信功能集成入APP中。
詳細功能介紹;
集成
下載SDK
創建新工程
創建新工程.jpg
將下載的IMSDK拖入到工程中
Libs.png
關于各SDK解釋
必選SDK:
必須是一個版本成套使用,不同版本不可混用
ImSDK.framework IM接口SDK
QALSDK.framework 網絡SDK
TLSSDK.framework 登錄SDK
可選SDK:
IMCore.framework IM核心功能
如果使用IM聊天必須加入,如果只用登錄功能(如只使用音視頻的情況,可不加入)
如果不加入IMCore.framework,使用時需 #import "ImSDK/ImSDKSimple.h",不要包含其他頭文件,否則可能會引起編譯錯誤
IMSDKBugly.framework Crash上報功能
如無特殊需要,推薦使用,在控制臺頁面可以查看Crash率等信息
如果不加入此SDK,需要調用 [TIMManager sharedInstance] disableCrashReport];
其他SDK:
QALHttpSDK.framework 網絡透傳SDK
只有用到網絡透傳功能時使用
添加系統依賴庫
CoreTelephony.framework
SystemConfiguration.framework
libc++.dylib
libz.dylib
libsqlite3.dylib
更改工程Other Linker Flags -> -ObjC
Other linker Flags.png
工程Info.plist文件增加
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
添加工程pch文件
導入一下頭文件
#import <ImSDK/ImSDK.h>
#import <QALSDK/QalSDKProxy.h>
#import <QALSDK/QalSDKCallbackProtocol.h>
#import "TLSSDK/TLSAccountHelper.h"
#import "TLSSDK/TLSLoginHelper.h"
#import "TLSSDK/TLSRefreshTicketListener.h"
#import "TLSSDK/TLSOpenLoginListener.h"
#import "TLSSDK/TLSHelper.h"
編譯工程,如果沒有報錯,繼續下一步
登錄騰訊開發者平臺
登錄開發者賬號.png
騰訊開發者需要實名認證
創建應用
創建應用.png
騰訊分配的會自動分配SdkAppId
帳號體系集成
- 獨立模式
- 托管模式
關于獨立模式和托管模式詳細的區別詳見
詳解
因為我這里不集成服務器,所以我選擇托管模式,由騰訊服務器負責保存用戶信息和負責用戶數據的校驗工作(注意每個創建的應用只能選擇一次模式,如果需要修改賬號體系,只能重新創建應用)
賬號體系集成.png
得到appid 和accountType
初始化SDK
在Appdelegate中
//初始化TIMSDK
TIMManager *manager = [TIMManager sharedInstance];
[manager disableCrashReport];
[manager initSdk:1400014178 accountType:@"7221"];
[[QalSDKProxy sharedInstance]initWithAppid:1400014178 andSDKAppid:1400014178 andAccType:7221];
TLSLoginHelper *helper = [[TLSLoginHelper getInstance]init:1400014178 andAccountType:7221 andAppVer:@"1.0"];
[[TLSAccountHelper getInstance]init:1400014178 andAccountType:7221 andAppVer:@"1.0"];
簡單搭建登錄界面
Simulator Screen Shot 2016年9月5日 下午5.44.14.png
注冊
- (IBAction)registerButtonClick:(id)sender
{
if (self.nameTextField.text.length == 0)
{
NSLog(@"請輸入用戶名");
return;
}else if (self.nameTextField.text.length > 24)
{
NSLog(@"用戶名最長不超過24字節");
return;
}
if (self.passWordTextField.text.length == 0)
{
NSLog(@"請輸入密碼");
return;
}else if (self.passWordTextField.text.length < 8)
{
NSLog(@"請輸入長度為8-16位密碼");
return;
}else if (self.passWordTextField.text.length > 16)
{
NSLog(@"請輸入長度為8-16位密碼");
return;
}
//注冊
/**
* 字符串賬號+密碼注冊接口
* @param account - 用戶輸入的賬號名,最長不得超過24字節
* @param password - 用戶輸入的密碼,密碼最短8字節,最長16字節
* @param listener - 回調接口 需要實現TLSStrAccountRegProtocol協議
* @return 0表示調用成功;其他表示調用失敗,返回碼見:_TLS_RETURN_VALUES
*/
[[TLSAccountHelper getInstance]TLSStrAccountReg:self.nameTextField.text andPassword:self.passWordTextField.text andTLSStrAccountRegListener:self];
//遵守TLSStrAccountRegListener 協議
}
SDK用戶名和密碼限制
- 用戶名 <24字節
- 8字節 <密碼 < 16字節
注冊成功回調
#pragma mark - TLSPwdLoginListener
- (void)OnStrAccountRegSuccess:(TLSUserInfo *)userInfo
{
NSLog(@"注冊成功---userInfo = %@",userInfo);
}
-(void)OnStrAccountRegFail:(TLSErrInfo *)errInfo
{
NSLog(@"注冊失敗---errInfo = %@",errInfo);
}
-(void)OnStrAccountRegTimeout:(TLSErrInfo *)errInfo
{
NSLog(@"注冊超時---errInfo = %@",errInfo);
}
登錄
- (IBAction)loginButtonClick:(id)sender
{
if (self.nameTextField.text.length == 0)
{
NSLog(@"請輸入用戶名");
return;
}else if (self.nameTextField.text.length > 24)
{
NSLog(@"用戶名最長不超過24字節");
return;
}
if (self.passWordTextField.text.length == 0)
{
NSLog(@"請輸入密碼");
return;
}else if (self.passWordTextField.text.length < 8)
{
NSLog(@"請輸入長度為8-16位密碼");
return;
}else if (self.passWordTextField.text.length > 16)
{
NSLog(@"請輸入長度為8-16位密碼");
}
//注意這里是登錄TLS,如果登錄成功后就進入主界面,請求會話或者好友列表會返回用戶沒有登錄錯誤,因此這里需要再TLS登錄成功后,繼續登錄IMSDK
int result = [[TLSLoginHelper getInstance]TLSPwdLogin:self.nameTextField.text andPassword:self.passWordTextField.text andTLSPwdLoginListener:self];
}
登錄回調
#pragma mark -TLSPwdLoginListener
/**
* 密碼登陸要求驗證圖片驗證碼
*
* @param picData 圖片驗證碼
* @param errInfo 錯誤信息
*/
-(void)OnPwdLoginNeedImgcode:(NSData *)picData andErrInfo:(TLSErrInfo *)errInfo
{
NSLog(@"登錄異常請輸入圖片驗證碼----errInfo = %@", errInfo);
}
/**
* 密碼登陸請求圖片驗證成功
*
* @param picData 圖片驗證碼
*/
-(void)OnPwdLoginReaskImgcodeSuccess:(NSData *)picData
{
NSLog(@"圖片驗證碼驗證成功---%@",picData);
}
/**
* 密碼登陸成功
*
* @param userInfo 用戶信息
*/
-(void)OnPwdLoginSuccess:(TLSUserInfo *)userInfo
{
NSLog(@"TLS 密碼登錄成功----errInfo = %@",userInfo);
//登錄
TIMLoginParam *login = [[TIMLoginParam alloc]init];
login.accountType = @"7221";
login.identifier = self.nameTextField.text;
login.userSig = [[TLSHelper getInstance] getTLSUserSig:userInfo.identifier];
login.sdkAppId = 1400014178;
login.appidAt3rd = @"1400014178";
//這里必須配置
[IMAPlatform configWith:login.config];
//直接登錄
[[HUDHelper sharedInstance] syncLoading:@"正在登錄"];
[[IMAPlatform sharedInstance] login:login succ:^{
[[HUDHelper sharedInstance] syncStopLoadingMessage:@"登錄成功"];
[UIApplication sharedApplication].keyWindow.rootViewController = [[KLTabBarController alloc]init];
[[IMAPlatform sharedInstance]configHost:login];//必須配置configHost
} fail:^(int code, NSString *msg) {
[[HUDHelper sharedInstance] syncStopLoadingMessage:IMALocalizedError(code, msg) delay:2 completion:^{
}];
NSLog(@"IM登錄失敗------%@",msg);
}];
}
/**
* 密碼登陸失敗
*
* @param errInfo 錯誤信息
*/
-(void)OnPwdLoginFail:(TLSErrInfo *)errInfo
{
NSLog(@"密碼登錄失敗----errInfo = %@",errInfo);
}
/**
* 密碼登陸超時
*
* @param errInfo 錯誤信息
*/
-(void)OnPwdLoginTimeout:(TLSErrInfo *)errInfo
{
NSLog(@"密碼登錄超時----errInfo = %@",errInfo);
}
實現自動登錄功能
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//獲取最后一些登錄的用戶
TLSUserInfo *user = [helper getLastUserInfo];
//判斷用戶是否需要自動登錄
BOOL login = [helper needLogin:user.identifier];
if (login == YES) {
//重新登錄
self.window.rootViewController = [[KLLoginOrRegisterViewController alloc]init];
}else
{//TLS 不需要登錄后,登錄TIMSDK
TIMLoginParam *login = [[TIMLoginParam alloc]init];
login.accountType = @"7221";
login.identifier = user.identifier;
login.userSig = [[TLSHelper getInstance] getTLSUserSig:user.identifier];
login.sdkAppId = 1400014178;
login.appidAt3rd = @"1400014178";
[IMAPlatform configWith:login.config];
//直接登錄
[[HUDHelper sharedInstance] syncLoading:@"正在登錄"];
[[IMAPlatform sharedInstance] login:login succ:^{
[[HUDHelper sharedInstance] syncStopLoadingMessage:@"登錄成功"];
[UIApplication sharedApplication].keyWindow.rootViewController = [[KLTabBarController alloc]init];
[[IMAPlatform sharedInstance]configHost:login];
} fail:^(int code, NSString *msg) {
[[HUDHelper sharedInstance] syncStopLoadingMessage:IMALocalizedError(code, msg) delay:2 completion:^{
}];
NSLog(@"IM登錄失敗------%@",msg);
}];
}
return YES;
}
歡迎github上下載demo下載地址