很屌的项目运用iOS学习总结xmpp

iOS基础--环信

2016-05-12  本文已影响1250人  云之君兮鹏
我回来了

简介:

1、libEaseMobClientSDKLite.a不包括实时语音功能
2、libEaseMobClientSDK.a包含所有功能
3、如果app中不需要实时语音功能,删掉libEaseMobClientSDK.a只使用libEaseMobClientSDKLite.a即可



初始化SDK

// 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[EaseMob sharedInstance]applicationDidEnterBackground:application];}
// 进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[EaseMob sharedInstance]applicationWillEnterForeground:application];}
// 申请处理时间
- (void)applicationWillTerminate:(UIApplication *)application {
[[EaseMob sharedInstance] applicationWillTerminate:application];
}`
//注册—异步的Block方式
- (IBAction)registerButtonAction:(id)sender {
[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.userNameTF.text password:self.passWordTF.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
if (error == nil) {
[self.delegate passValueUserName:self.userNameTF.text PassWord:self.passWordTF.text];
[self dismissViewControllerAnimated:YES completion:nil]; }
onQueue:dispatch_get_main_queue()];
}
// 登录---异步的Block方法
- (IBAction)loginButtonAction:(id)sender {
[[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userNameTF.text password:self.passWordTF.text completion:^(NSDictionary *loginInfo, EMError *error) {
if (error == nil) {
AppDelegate *app = [UIApplication sharedApplication].delegate;
[app createTabBar];
NSLog(@"登录成功"); } }
onQueue:dispatch_get_main_queue()];}

// 添加好友 ---
- (IBAction)addRosterAction:(id)sender {
EMError *error = nil;
BOOL succeed = [[EaseMob sharedInstance].chatManager addBuddy:self.nameTF.text message:@"约吗?" error:&error];
if (succeed && !error) {
[self.navigationController popToRootViewControllerAnimated:YES]; }}


#pragma mark ---EMChatManagerDelegate 代理方法


 //设置代理

 [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:dispatch_get_main_queue()];

// 收到好友请求
- (void)didReceiveBuddyRequest:(NSString *)username message:(NSString *)message{

NSString *title = [NSString stringWithFormat:@"%@ 跪求添加您为好友", username]; 

UIAlertController *controller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];

UIAlertAction *actionAgree = [UIAlertAction actionWithTitle:@"行吧" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {       

//环信里错误类 相当于NSError

 EMError *error = nil;
 BOOL succeed = [[EaseMob sharedInstance].chatManager acceptBuddyRequest:username error:&error];       
 if (succeed && !error) {            
NSLog(@"接收成功");        } else 
{            NSLog(@"接收失败");        }    }];  

UIAlertAction *actionDisagree = [UIAlertAction actionWithTitle:@"丑拒" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {              
 EMError *error = nil;       
 BOOL succeed = [[EaseMob sharedInstance].chatManager rejectBuddyRequest:username reason:@"呵呵呵" error:&error];        
if (succeed && !error) {            
NSLog(@"已拒绝成功"); }
 else {  NSLog(@"拒绝失败");        }    }];     
 [controller addAction:actionAgree];    
[controller addAction:actionDisagree];   
 [self presentViewController:controller animated:YES completion:nil];}

//发送消息
- (void)sendMessage{
EMChatText *text = [[EMChatText alloc]initWithText:@"收到请回复,OVER"];
EMTextMessageBody *textMessageBody = [[EMTextMessageBody alloc]initWithChatObject:text];

 EMMessage *message = [[EMMessage alloc]initWithReceiver:self.chatter bodies:@[textMessageBody]]; 

// 发送这条消息

[[EaseMob sharedInstance].chatManager asyncSendMessage:message progress:nil prepare:^(EMMessage *message, EMError *error)
 {     
 //准备发送   
 } 
onQueue:dispatch_get_main_queue() completion:^(EMMessage *message, EMError *error) 
{       
 // 发送成功        
if (!error) {            
[self reloadMessage];        }    }   
 onQueue:dispatch_get_main_queue()];}

// 接收到消息

- (void)didReceiveMessage:(EMMessage *)message{  
  [self reloadMessage];
}

// 获取某聊天对象的所有聊天信息

- (void)reloadMessage{      
 self.dataArray = [[EaseMob sharedInstance].chatManager conversationForChatter:self.chatter conversationType:(eConversationTypeChat)];

}

上一篇 下一篇

猜你喜欢

热点阅读