环信的基本应用

1. 准备工作

和其他百度地图、极光推送用法相似,都需要在官网上注册开发者账号,然后创建应用并分配一个key,制作推送证书上传。

  • 注册环信开发者账号并创建后台应用
  • 制作并上传推送证书

2.基础功能

2.1 导入SDK

导入有2种做法,一种使用cocoapod,一种是下载SDK导入,推荐使用cocoapod方法,可以不需要进行复杂的依赖设置,简单快捷

2.2 基础功能

  • 初始化 SDK
    在工程的 AppDelegate.m 中的以下方法中,调用 SDK 对应方法。
// 环信配置
    EMOptions *options = [EMOptions optionsWithAppkey:@"1155170406115327#ggchat1"];
    options.apnsCertName = @"istore_dev";
    [[EMClient sharedClient] initializeSDKWithOptions:options];
    
    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    
    // 如果设置了自动登陆,则直接跳转主界面
    if ([EMClient sharedClient].options.isAutoLogin) {
        self.window.rootViewController = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
    }
    
    return YES;
  • 注册账号
    注册模式分两种,开放注册和授权注册。
    开放注册调用以下方法:
[[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:@"登陆成功"];
            // 跳转界面
            [UIApplication sharedApplication].keyWindow.rootViewController = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
            
            // 设置环信自动登陆
            [[EMClient sharedClient].options setIsAutoLogin:YES];
        } else {
//            NSLog(@"登陆失败,%@",aError.errorDescription);
//            NSString *errorStr = =aError.errorDescription;
            [SVProgressHUD showErrorWithStatus:aError.errorDescription];
        }
    }];

登陆演示:

登陆演示.gif

你可能感兴趣的:(环信的基本应用)