首页 > 其他 > 详细

对环信的封装--初级

时间:2014-08-19 20:48:35      阅读:525      评论:0      收藏:0      [点我收藏+]

@interface EaseMobProcessor : NSObject

 +(void)    init:(UIApplication *)application

  launchOptions:(NSDictionary *)launchOptions;

 +(void) login;

 +(void) logout;

 +(void) registeDeviceToken:(UIApplication *)application deviceToken:(NSData *)deviceToken;

 +(void) applicationWillResignActive:(UIApplication *)application;

 +(void) applicationDidEnterBackground:(UIApplication *)application;

 +(void) applicationWillEnterForeground:(UIApplication *)application;

 +(void) applicationDidBecomeActive:(UIApplication *)application;

 +(void) applicationWillTerminate:(UIApplication *)application;

@end


 

EaseMobProcessor.m中

#import "EaseMobProcessor.h"

#import "EaseMob.h"

#import "AppStatus.h"

#import "UserStore.h"

@implementation EaseMobProcessor

+(void) init:(UIApplication *)application launchOptions:(NSDictionary *)launchOptions{

    //注册 APNS文件的名字, 需要与后台上传证书时的名字一一对应

NSString *apnsCertName = [AppStatus sharedInstance].easemobApnsCertName;

    NSLog(@">>>>>>ease mob config: %@, %@", apnsCertName, [AppStatus sharedInstance].easemobAppKey);

[[EaseMob sharedInstance] registerSDKWithAppKey:[AppStatus sharedInstance].easemobAppKey apnsCertName:apnsCertName];

[[EaseMob sharedInstance] enableBackgroundReceiveMessage];

[[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];

}

//登录时调用 

+(void) login{

    AppStatus *as = [AppStatus sharedInstance];

    if (![as logined] || as.easeMobLogined) {

        return ;

    }

//要注意这里的判断![as.user.easemobUserUuid isEqual:[NSNull null]

    if (as.user.easemobUserUuid != nil && ![as.user.easemobUserUuid isEqual:[NSNull null]]  

                                       && ![as.user.easemobUserUuid isEqualToString:@""]) {

        NSLog(@">>>> easemob username:%@, pwd:%@", as.user.userCode, as.user.accessToken);

        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:as.user.userCode

                                                            password:as.user.accessToken

                                                          completion:

         ^(NSDictionary *loginInfo, EMError *error) {

             if (error) {

                 NSLog(@"登录环信失败:%@", error);

             }else {

                 NSLog(@"登录环信成功");

                 as.easeMobLogined = YES;

                 [AppStatus saveAppStatus];

             }

         } onQueue:nil];

    }else{

        [NSThread sleepForTimeInterval:2];

        [UserStore getMyUserInfo:^(User *user, NSError *err) {

            as.user.easemobUserUuid = user.easemobUserUuid;

            [EaseMobProcessor login];

        }];

    }

}

 //  退出是调用

+(void) logout{

    AppStatus *as = [AppStatus sharedInstance];

    [[EaseMob sharedInstance].chatManager asyncLogoff];

    as.easeMobLogined = NO;

    [AppStatus saveAppStatus];

 

}

 

+(void) registeDeviceToken:(UIApplication *)application deviceToken:(NSData *)deviceToken{

    // 让SDK得到App目前的各种状态,以便让SDK做出对应当前场景的操作

[[EaseMob sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

}

 

+(void) applicationWillResignActive:(UIApplication *)application{

    // 让SDK得到App目前的各种状态,以便让SDK做出对应当前场景的操作

[[EaseMob sharedInstance] applicationWillResignActive:application];

}

 

+(void) applicationDidEnterBackground:(UIApplication *)application{

    // 让SDK得到App目前的各种状态,以便让SDK做出对应当前场景的操作

[[EaseMob sharedInstance] applicationDidEnterBackground:application];

}

 

+(void) applicationWillEnterForeground:(UIApplication *)application{

    // 让SDK得到App目前的各种状态,以便让SDK做出对应当前场景的操作

[[EaseMob sharedInstance] applicationWillEnterForeground:application];

}

 

+(void) applicationDidBecomeActive:(UIApplication *)application{

    // 让SDK得到App目前的各种状态,以便让SDK做出对应当前场景的操作

[[EaseMob sharedInstance] applicationDidBecomeActive:application];

    [EaseMobProcessor login];

}

 

+(void) applicationWillTerminate:(UIApplication *)application{

    // 让SDK得到App目前的各种状态,以便让SDK做出对应当前场景的操作

[[EaseMob sharedInstance] applicationWillTerminate:application];

}

 

@end

对环信的封装--初级,布布扣,bubuko.com

对环信的封装--初级

原文:http://www.cnblogs.com/xubojoy/p/3922977.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!