1. 準備工作
和其他百度地圖、極光推送用法相似掉丽,都需要在官網(wǎng)上注冊開發(fā)者賬號跌榔,然后創(chuàng)建應用并分配一個key,制作推送證書上傳捶障。
- 注冊環(huán)信開發(fā)者賬號并創(chuàng)建后臺應用
- 制作并上傳推送證書
2.基礎功能
2.1 導入SDK
導入有2種做法僧须,一種使用cocoapod,一種是下載SDK導入项炼,推薦使用cocoapod方法皆辽,可以不需要進行復雜的依賴設置柑蛇,簡單快捷
2.2 基礎功能
- 初始化 SDK
在工程的 AppDelegate.m 中的以下方法中,調(diào)用 SDK 對應方法驱闷。
// 環(huán)信配置
EMOptions *options = [EMOptions optionsWithAppkey:@"1155170406115327#ggchat1"];
options.apnsCertName = @"istore_dev";
[[EMClient sharedClient] initializeSDKWithOptions:options];
[[EMClient sharedClient] addDelegate:self delegateQueue:nil];
// 如果設置了自動登陸耻台,則直接跳轉(zhuǎn)主界面
if ([EMClient sharedClient].options.isAutoLogin) {
self.window.rootViewController = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
}
return YES;
- 注冊賬號
注冊模式分兩種,開放注冊和授權注冊空另。
開放注冊調(diào)用以下方法:
[[EMClient sharedClient] registerWithUsername:username password:password completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"注冊成功,%@",aUsername);
[SVProgressHUD showSuccessWithStatus:@"注冊成功"];
} else {
NSLog(@"注冊失敗,%@",aError.errorDescription);
NSString *errorInfo = [NSString stringWithFormat:@"注冊失敗%@",aError.errorDescription];
[SVProgressHUD showErrorWithStatus:errorInfo];
}
}];
注冊演示:
注冊演示.gif
- 登陸
[[EMClient sharedClient] loginWithUsername:username password:password completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"登陸成功,%@",aUsername);
// [SVProgressHUD showSuccessWithStatus:@"登陸成功"];
// 跳轉(zhuǎn)界面
[UIApplication sharedApplication].keyWindow.rootViewController = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
// 設置環(huán)信自動登陸
[[EMClient sharedClient].options setIsAutoLogin:YES];
} else {
// NSLog(@"登陸失敗,%@",aError.errorDescription);
// NSString *errorStr = =aError.errorDescription;
[SVProgressHUD showErrorWithStatus:aError.errorDescription];
}
}];
登陸演示:
登陸演示.gif