-
Notifications
You must be signed in to change notification settings - Fork 0
/
EMIMHelper.m
executable file
·141 lines (117 loc) · 4.49 KB
/
EMIMHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// EMIMHelper.m
// CustomerSystem-ios
//
// Created by dhc on 15/3/28.
// Copyright (c) 2015年 easemob. All rights reserved.
//
#import "EMIMHelper.h"
#import "EaseMob.h"
#import "LocalDefine.h"
#import "Daenerys-Swift.h"
static EMIMHelper *helper = nil;
@implementation EMIMHelper
@synthesize appkey = _appkey;
@synthesize cname = _cname;
@synthesize username = _username;
@synthesize password = _password;
#define kEaseMobUserName @"kEaseMobUserName"
#define kEaseMobUserPassword @"kEaseMobUserPassword"
- (instancetype)init
{
self = [super init];
if (self) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
_appkey = [userDefaults objectForKey:kAppKey];
if ([_appkey length] == 0) {
_appkey = kDefaultAppKey;
[userDefaults setObject:_appkey forKey:kAppKey];
}
_cname = [userDefaults objectForKey:kCustomerName];
if ([_cname length] == 0) {
_cname = kDefaultCustomerName;
[userDefaults setObject:_cname forKey:kCustomerName];
}
_username = [userDefaults objectForKey:kEaseMobUserName];
_password = [userDefaults objectForKey:kEaseMobUserPassword];
}
return self;
}
+ (instancetype)defaultHelper
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[EMIMHelper alloc] init];
});
return helper;
}
#pragma mark - login
- (void)loginEasemobSDK
{
EaseMob *easemob = [EaseMob sharedInstance];
if ([easemob.chatManager isLoggedIn]) {
[easemob.chatManager logoffWithUnbindDeviceToken:false error:nil];
[self refreshHelperData];
}
if (![easemob.chatManager isLoggedIn] || ([_username length] == 0 || [_password length] == 0)) {
if ([_username length] == 0 || [_password length] == 0) {
if ([RBAAccountKit sharedManager].isLogin) {
_username = [NSString stringWithFormat:@"%@", [RBAAccountKit sharedManager].loginUserID];
_password = @"ricebook123";
}else {
UIDevice *device = [UIDevice currentDevice];//创建设备对象
NSString *deviceUID = [[NSString alloc] initWithString:[[device identifierForVendor] UUIDString]];
if ([deviceUID length] == 0) {
CFUUIDRef uuid = CFUUIDCreate(NULL);
if (uuid)
{
deviceUID = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
}
}
NSString *joinString = [NSString stringWithFormat:@"iOS%@", [deviceUID stringByReplacingOccurrencesOfString:@"-" withString:@""]];
_username = joinString;
_password = @"ricebook123";
}
[easemob.chatManager asyncRegisterNewAccount:_username password:_password withCompletion:^(NSString *username, NSString *password, EMError *error) {
if (!error || error.errorCode == EMErrorServerDuplicatedAccount) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:username forKey:kEaseMobUserName];
[userDefaults setObject:password forKey:kEaseMobUserPassword];
[easemob.chatManager asyncLoginWithUsername:_username password:_password];
}
} onQueue:nil];
}
else{
[easemob.chatManager asyncLoginWithUsername:_username password:_password];
}
}
}
#pragma mark - info
- (void)setCname:(NSString *)cname
{
if ([cname length] > 0 && ![cname isEqualToString:_cname]) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:cname forKey:kCustomerName];
_cname = cname;
}
}
- (void)refreshHelperData
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
_appkey = [userDefaults objectForKey:kAppKey];
if ([_appkey length] == 0) {
_appkey = kDefaultAppKey;
[userDefaults setObject:_appkey forKey:kAppKey];
}
_cname = [userDefaults objectForKey:kCustomerName];
if ([_cname length] == 0) {
_cname = kDefaultCustomerName;
[userDefaults setObject:_cname forKey:kCustomerName];
}
[userDefaults removeObjectForKey:kEaseMobUserName];
[userDefaults removeObjectForKey:kEaseMobUserPassword];
_username = nil;
_password = nil;
}
@end