宽容他人,放过自己。

环信集成-基于环信的注册登陆系统

Posted on By anchoriteFili

752372-20151208180542590-1466607057.png

集成过程


1. 导入环信demo部分
pod 'EaseMobSDKFull', :git => 'https://github.com/easemob/sdk-ios-cocoapods-integration.git'
2. 在appdelegate中引入头文件#import <EaseMobSDKFull/EaseMob.h>然后就是一路的复制粘贴了
#import <EaseMobSDKFull/EaseMob.h>

#pragma mark 环信信息基本注册
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    //registerSDKWithAppKey:注册的appKey,详细见下面注释。
    //apnsCertName:推送证书名(不需要加后缀),详细见下面注释。
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"anchoritefiligod#testone" apnsCertName:nil];
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

#pragma mark App进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    [[EaseMob sharedInstance] applicationDidEnterBackground:application];
}

#pragma mark app将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];
}

#pragma mark 申请处理时间
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
    [[EaseMob sharedInstance] applicationWillTerminate:application];
}

登陆系统


#import <EaseMobSDKFull/EaseMob.h>

#define SETUSERDEFAULT(VALUE, KEY) [[NSUserDefaults standardUserDefaults] setObject:(VALUE) forKey:(KEY)]
#define GETUSERDEFAULT(KEY) [[NSUserDefaults standardUserDefaults] objectForKey:(KEY)]

#import "ViewController.h"
#import <EaseMobSDKFull/EaseMob.h>

#define SETUSERDEFAULT(VALUE, KEY) [[NSUserDefaults standardUserDefaults] setObject:(VALUE) forKey:(KEY)]
#define GETUSERDEFAULT(KEY) [[NSUserDefaults standardUserDefaults] objectForKey:(KEY)]


@interface ViewController ()<EMChatManagerDelegate>

@property (weak, nonatomic) IBOutlet UITextField *zhanghao;
@property (weak, nonatomic) IBOutlet UITextField *mima;
@property (weak, nonatomic) IBOutlet UIButton *zidongdengluButton;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
#pragma mark 自动登陆部分
    BOOL isAutoLogin = [[EaseMob sharedInstance].chatManager isAutoLoginEnabled];
    
    NSLog(@"isAutoLogin ====== %d",isAutoLogin);
    
    if (isAutoLogin) {
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:GETUSERDEFAULT(@"account")
                                                            password:GETUSERDEFAULT(@"Password")
                                                          completion:^(NSDictionary *loginInfo, EMError *error) {
                                                              NSLog(@"自动登陆");
                                                          } onQueue:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 注册
- (IBAction)zhuce:(UIButton *)sender {
    
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.zhanghao.text password:self.mima.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        if (!error) {
            NSLog(@"注册成功");
        }
    } onQueue:nil];
}

#pragma mark 登陆
- (IBAction)denglu:(UIButton *)sender {
    
#pragma mark 登陆
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.zhanghao.text password:self.mima.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {
            NSLog(@"登陆成功");
        }
    } onQueue:nil];

    SETUSERDEFAULT(self.zhanghao.text, @"account");
    SETUSERDEFAULT(self.mima.text, @"Password");
    
#pragma mark 设置是否自动登陆
    if ([self.zidongdengluButton.currentTitle isEqualToString:@"是"]) {
        
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.zhanghao.text password:self.mima.text completion:^(NSDictionary *loginInfo, EMError *error) {
            if (!error) {
                // 设置自动登录
                [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            }
        } onQueue:nil];
    }
}

#pragma mark 是否自动登陆
- (IBAction)zidongdenglu:(UIButton *)sender {
    
    if ([sender.currentTitle isEqualToString:@"是"]) {
    
        [sender setTitle:@"否" forState:UIControlStateNormal];
    } else {
        [sender setTitle:@"是" forState:UIControlStateNormal];
    }
}

#pragma mark 退出点击事件
- (IBAction)tuichuButton:(UIButton *)sender {
    //在被动退出时传NO,在主动退出时传YES.
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:NO completion:^(NSDictionary *info, EMError *error) {
        if (!error && info) {
            NSLog(@"退出成功");
        }
    } onQueue:nil];
}

/*!
 @method
 @brief 当前登录账号在其它设备登录时的通知回调
 @discussion
 @result
 */
#pragma mark 从其他机器上登陆了本账号
- (void)didLoginFromOtherDevice {
    
    //在被动退出时传NO,在主动退出时传YES.
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:NO completion:^(NSDictionary *info, EMError *error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"从其他机器上登陆了本账号"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil,
                                  nil];
        [alertView show];
    } onQueue:nil];
}

/*!
 @method
 @brief 当前登录账号已经被从服务器端删除
 @discussion
 @result
 */
#pragma mark 当前登录账号已经被从服务器端删除
- (void)didRemovedFromServer {
    
    //在被动退出时传NO,在主动退出时传YES.
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:NO completion:^(NSDictionary *info, EMError *error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"当前登录账号已经被从服务器端删除"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil,
                                  nil];
        [alertView show];
    } onQueue:nil];
}

/*!
 @method
 @brief 将要发起自动重连操作时发送该回调
 @discussion
 @result
 */

- (void)willAutoReconnect {
    
    NSLog(@"将要重连");
}

/*!
 @method
 @brief 自动重连操作完成后的回调(成功的话,error为nil,失败的话,查看error的错误信息)
 @discussion
 @result
 */
- (void)didAutoReconnectFinishedWithError:(NSError *)error {
    
    NSLog(@"重连完成");
}

@end