• 环信框架使用


    1、配置对象

    EMOptions对象环信的配置对象,其中appKey、推送证书名、自动登录功能等进行设置

    2、登录模块

    环信初始化:
    
    // 在application:didFinishLaunchingWithOptions:方法中初始化环信
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        //AppKey:注册的AppKey,详细见下面注释。
        //apnsCertName:推送证书名(不需要加后缀),详细见下面注释。
        EMOptions *options = [EMOptions optionsWithAppkey:@"douser#istore"];
        options.apnsCertName = @"istore_dev";
        [[EMClient sharedClient] initializeSDKWithOptions:options];
    
        return YES;
    }
    
    // 登录环信
    EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"];
    if (!error) {
        NSLog(@"登录成功");
    }

    3、登录功能的代理模块

     [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    设置登录模块的代理对象(遵循EMClientDelegate协议)
    实现下面的4个方法可以监听登录状态
    // 判断手机是否联网,网络变化时会调用该方法
    - (void)connectionStateDidChange:(EMConnectionState)aConnectionState;
    // 自动登录完成后,调用该方法
    - (void)autoLoginDidCompleteWithError:(EMError *)aError;
    // 账号在其他设备上登录后,调用该方法,自动退出登录,无需手动退出
    - (void)userAccountDidLoginFromOtherDevice;
    // 账号被服务器禁用后,调用该方法。自动退出
    - (void)userAccountDidRemoveFromServer;

    4、自动登录与被动退出

    当出现下面的情况时,EMOptions对象中的isAutoLogin参数为false。
     1、用户调用了 SDK 的登出动作;
     2、用户在别的设备上更改了密码,导致此设备上自动登录失败;
     3、用户的账号被从服务器端删除;
     4、用户从另一个设备登录,把当前设备上登录的用户踢出;
    
    当出现下面的情况,被动退出
     1、用户从另一个设备登录,把当前设备上登录的用户踢出;
     2、用户的账号被从服务器端删除;

    5、退出

    // YES表示解除DeviceToken绑定,该手机将不会接受到消息推送。
    EMError *error = [[EMClient sharedClient] logout:YES];
    if (!error) {
         NSLog(@"退出成功");
    }

    6、代码写法

    // 判断是否设置自动登录,false则登录环信
    BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin; if (!isAutoLogin) { [[EMClient sharedClient] loginWithUsername:@"admin" password:@"123456" completion:^(NSString *aUsername, EMError *aError) { if (!aError) { EMClient *c = [EMClient sharedClient]; [EMClient sharedClient].options.isAutoLogin = YES; // 开启自动登录 [[EMClient sharedClient] addDelegate:self delegateQueue:nil]; // 设置代理方法,队列为nil则在主线程中调用。 NSLog(@"登录成功"); }else{ NSLog(@"登录失败"); } }]; }
  • 相关阅读:
    centOS7 mini配置linux服务器(三) 配置防火墙以及IPtables切换
    Linux的常用基本命令。
    centOS7 mini配置linux服务器(二) 配置IP
    分享Git的一些个人配置
    Firefox中Vimperator插件配置
    Linux下修改键盘默认布局
    Git对于单个文件的分批提交方式的使用
    Linux安装Monaco字体
    浮点与整形在GUI下的相关思考
    2D简单图形相关算法罗列
  • 原文地址:https://www.cnblogs.com/Zp3sss/p/9086619.html
Copyright © 2020-2023  润新知