@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
对环信的封装--初级