环信即时通讯的基本集成

项目中是基于环信SDK2.0集成开发,所以本文只介绍SDK2.0的基本集成.

第一,前期准备工作,上传推送证书,注册环信开发者账号.然后开始导入环信SDK,EaseMobSDK,导入环信文档2.x需求的SDK依赖库.

第二,向Build Settings → Linking → Other Linker Flags 中添加-force_load[空格]EaseMobSDK/lib/libEaseMobClientSDKLite.a(静态库的路径)

第三, SDK不支持bitcode,向Build Settings → Linking → Enable Bitcode中设置NO(在2.0没有提到这个,但是在3.0提到了这个问题,最好使用2.0的时候也这样设置)

下面来说下基于环信demo集成的经验.

首先在#import "AppDelegate.h"代理方法注册环信    [[EaseMob sharedInstance] registerSDKWithAppKey:@"" apnsCertName:@""l];

在APP首次登陆的时候进入登陆页面,注册账号

    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.nameField.text password:self.pwdField.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        if (!error) {
            NSLog(@"注册成功");
        }
    } onQueue:nil];
根据自己注册的账号登陆进APP,
    if (self.nameField.text.length&&self.pwdField.text.length) {
        
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.nameField.text password:self.pwdField.text completion:^(NSDictionary *loginInfo, EMError *error) {
            if (loginInfo&&!error) {
                
                //设置是否自动登陆
                [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
                
                //旧数据转换
                [[EaseMob sharedInstance].chatManager importDataToNewDatabase];
                
                //获取数据库中的数据
                [[EaseMob sharedInstance].chatManager loadDataFromDatabase];
                
                //获取群组列表
                [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsList];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
                
                UserModel *model = mySelf;
                model.userId = self.nameField.text;
                model.hxPasswd = self.pwdField.text;
                
                //保存用户信息到本地
                [UserModel saveMySelf];
                
                [self showHint:@"登陆成功"];
            }
        } onQueue:nil];

        
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginStateChange" object:@(YES)];

        

    }else{
        [self showHint:@"请检查账号和密码"];
    }
在这里一定要注意block块中设置的是否为自动登陆等属性,并发出登陆状态改变的通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"loginStateChange" object:@(YES)];在每次APP登陆的时候在AppDelegate中监听登陆状态的改变
   [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginStateChange:)
                                                 name:@"loginStateChange"
                                               object:nil];
在下面的方法中判断登陆的页面
-(void)loginStateChange:(NSNotification *)notification
{
    UINavigationController *nav = nil ;
    
    BOOL isAutoLogin = [[EaseMob sharedInstance].chatManager isAutoLoginEnabled];
    BOOL loginSuccess = [notification.object boolValue];
    
    if (isAutoLogin || loginSuccess) {//登陆成功加载主窗口控制器
        //加载申请通知的数据
        [[ApplyViewController shareController] loadDataSourceFromLocalDB];
        
        if (_mainController == nil) {
            _mainController = [[MainViewController alloc] init];
            [_mainController networkChanged:_connectionState];
            nav = [[UINavigationController alloc] initWithRootViewController:_mainController];
        }else{
            nav = _mainController.navigationController;
        }
        
    }else{//登陆失败加载登陆页面控制器
        
        _mainController = nil;
        ViewController *loginController = [[ViewController alloc] init];
        nav = [[UINavigationController alloc] initWithRootViewController:loginController];
        loginController.title = NSLocalizedString(@"AppName", @"EaseMobDemo");
        
        [self clearParse];
    }
    if ([UIDevice currentDevice].systemVersion.floatValue < 7.0) {
        nav.navigationBar.barStyle = UIBarStyleDefault;
        [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"titleBar"] forBarMetrics:UIBarMetricsDefault];
        [nav.navigationBar.layer setMasksToBounds:YES];
    }
    self.window.rootViewController = nav;
    [nav setNavigationBarHidden:YES];
    [nav setNavigationBarHidden:NO];
    
}
然后重写demo中提供的MainViewController.h的方法,其继承于UITabBarController,
- (void)jumpToChatList;

- (void)setupUntreatedApplyCount;

- (void)networkChanged:(EMConnectionState)connectionState;

- (void)didReceiveLocalNotification:(UILocalNotification *)notification;

对于上述提供的方法可以选择的使用,对于不需要的可以注释掉.


在项目中使用ChatListViewController.h获取好友列表,并且显示好友信息和消息的未读数

使用ChatViewController.h获得会话控制器,可以是群组聊天也可以是单个的聊天

下面截图显示在项目中用到的第三方框架与分类





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值