TUILogin.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // Created by Tencent on 2023/06/09.
  2. // Copyright © 2023 Tencent. All rights reserved.
  3. #import "TUILogin.h"
  4. #import "TUICore.h"
  5. #import "TUIDefine.h"
  6. @import ImSDK_Plus;
  7. NSString *const TUIInitSdkSuccessNotification = @"TUIInitSdkSuccessNotification";
  8. NSString *const TUIInitSdkFailNotification = @"TUIInitSdkFailNotification";
  9. NSString *const TUILoginSuccessNotification = @"TUILoginSuccessNotification";
  10. NSString *const TUILoginFailNotification = @"TUILoginFailNotification";
  11. NSString *const TUILogoutSuccessNotification = @"TUILogoutSuccessNotification";
  12. NSString *const TUILogoutFailNotification = @"TUILogoutFailNotification";
  13. @implementation TUILoginConfig
  14. - (instancetype)init {
  15. if (self = [super init]) {
  16. self.logLevel = TUI_LOG_INFO;
  17. }
  18. return self;
  19. }
  20. @end
  21. @interface TUILogin () <V2TIMSDKListener>
  22. @property(nonatomic, strong) NSHashTable *loginListenerSet;
  23. @property(nonatomic, assign) int sdkAppID;
  24. @property(nonatomic, copy) NSString *userID;
  25. @property(nonatomic, copy) NSString *userSig;
  26. @property(nonatomic, copy) NSString *nickName;
  27. @property(nonatomic, copy) NSString *faceUrl;
  28. @property(nonatomic, assign) BOOL loginWithInit;
  29. @property(nonatomic, assign) TUIBusinessScene currentBusinessScene;
  30. @end
  31. @implementation TUILogin
  32. #pragma mark - API
  33. + (void)initWithSdkAppID:(int)sdkAppID {
  34. [TUILogin.shareInstance initWithSdkAppID:sdkAppID];
  35. }
  36. + (void)login:(NSString *)userID userSig:(NSString *)userSig succ:(TSucc)succ fail:(TFail)fail {
  37. [TUILogin.shareInstance login:userID userSig:userSig succ:succ fail:fail];
  38. }
  39. + (void)login:(int)sdkAppID userID:(NSString *)userID userSig:(NSString *)userSig succ:(TSucc)succ fail:(TFail)fail {
  40. [TUILogin.shareInstance login:sdkAppID userID:userID userSig:userSig config:nil succ:succ fail:fail];
  41. }
  42. + (void)login:(int)sdkAppID userID:(NSString *)userID userSig:(NSString *)userSig config:(TUILoginConfig *)config succ:(TSucc)succ fail:(TFail)fail {
  43. [TUILogin.shareInstance login:sdkAppID userID:userID userSig:userSig config:config succ:succ fail:fail];
  44. }
  45. + (void)getSelfUserInfo {
  46. [TUILogin.shareInstance getSelfUserInfo];
  47. }
  48. + (void)logout:(TSucc)succ fail:(TFail)fail {
  49. [TUILogin.shareInstance logout:succ fail:fail];
  50. }
  51. + (void)addLoginListener:(id<TUILoginListener>)listener {
  52. [TUILogin.shareInstance addLoginListener:listener];
  53. }
  54. + (void)removeLoginListener:(id<TUILoginListener>)listener {
  55. [TUILogin.shareInstance removeLoginListener:listener];
  56. }
  57. + (int)getSdkAppID {
  58. return [TUILogin.shareInstance getSdkAppID];
  59. }
  60. + (BOOL)isUserLogined {
  61. return [V2TIMManager sharedInstance].getLoginStatus == V2TIM_STATUS_LOGINED;
  62. }
  63. + (NSString *)getUserID {
  64. return [TUILogin.shareInstance getUserID];
  65. }
  66. + (NSString *)getUserSig {
  67. return [TUILogin.shareInstance getUserSig];
  68. }
  69. + (NSString *)getNickName {
  70. return [TUILogin.shareInstance getNickName];
  71. }
  72. + (NSString *)getFaceUrl {
  73. return [TUILogin.shareInstance getFaceUrl];
  74. }
  75. + (void)setCurrentBusinessScene:(TUIBusinessScene)scene {
  76. TUILogin.shareInstance.currentBusinessScene = scene;
  77. }
  78. + (TUIBusinessScene)getCurrentBusinessScene {
  79. return TUILogin.shareInstance.currentBusinessScene;
  80. }
  81. #pragma mark - Private
  82. + (instancetype)shareInstance {
  83. static id gShareInstance = nil;
  84. static dispatch_once_t onceToken;
  85. dispatch_once(&onceToken, ^{
  86. gShareInstance = [[self alloc] init];
  87. });
  88. return gShareInstance;
  89. }
  90. - (instancetype)init {
  91. if (self = [super init]) {
  92. _loginListenerSet = [NSHashTable weakObjectsHashTable];
  93. _sdkAppID = 0;
  94. _userID = nil;
  95. _userSig = nil;
  96. _nickName = nil;
  97. _faceUrl = nil;
  98. _loginWithInit = NO;
  99. _currentBusinessScene = None;
  100. }
  101. return self;
  102. }
  103. - (void)initWithSdkAppID:(int)sdkAppID {
  104. if (0 != self.sdkAppID && sdkAppID != self.sdkAppID) {
  105. [self logout:nil fail:nil];
  106. [[V2TIMManager sharedInstance] unInitSDK];
  107. }
  108. self.sdkAppID = sdkAppID;
  109. V2TIMSDKConfig *config = [[V2TIMSDKConfig alloc] init];
  110. config.logLevel = V2TIM_LOG_INFO;
  111. if ([[V2TIMManager sharedInstance] initSDK:sdkAppID config:config listener:nil]) {
  112. [NSNotificationCenter.defaultCenter postNotificationName:TUIInitSdkSuccessNotification object:nil];
  113. } else {
  114. [NSNotificationCenter.defaultCenter postNotificationName:TUIInitSdkFailNotification object:nil];
  115. }
  116. }
  117. - (void)login:(NSString *)userID userSig:(NSString *)userSig succ:(TSucc)succ fail:(TFail)fail {
  118. self.userID = userID;
  119. self.userSig = userSig;
  120. self.loginWithInit = NO;
  121. self.currentBusinessScene = None;
  122. if ([[[V2TIMManager sharedInstance] getLoginUser] isEqualToString:userID]) {
  123. if (succ) {
  124. succ();
  125. }
  126. [NSNotificationCenter.defaultCenter postNotificationName:TUILoginSuccessNotification object:nil];
  127. } else {
  128. __weak __typeof(self) weakSelf = self;
  129. [[V2TIMManager sharedInstance] login:userID
  130. userSig:userSig
  131. succ:^{
  132. __strong __typeof(weakSelf) strongSelf = weakSelf;
  133. [strongSelf getSelfUserInfo];
  134. if (succ) {
  135. succ();
  136. }
  137. [NSNotificationCenter.defaultCenter postNotificationName:TUILoginSuccessNotification object:nil];
  138. }
  139. fail:^(int code, NSString *desc) {
  140. if (fail) {
  141. fail(code, desc);
  142. }
  143. [NSNotificationCenter.defaultCenter postNotificationName:TUILoginFailNotification object:nil];
  144. }];
  145. }
  146. }
  147. - (void)login:(int)sdkAppID userID:(NSString *)userID userSig:(NSString *)userSig config:(TUILoginConfig *)config succ:(TSucc)succ fail:(TFail)fail {
  148. self.loginWithInit = YES;
  149. self.currentBusinessScene = None;
  150. if (0 != self.sdkAppID && sdkAppID != self.sdkAppID) {
  151. [self logout:nil fail:nil];
  152. [[V2TIMManager sharedInstance] unInitSDK];
  153. }
  154. self.sdkAppID = sdkAppID;
  155. self.userID = userID;
  156. self.userSig = userSig;
  157. V2TIMSDKConfig *sdkConfig = [[V2TIMSDKConfig alloc] init];
  158. if (config != nil) {
  159. sdkConfig.logLevel = (V2TIMLogLevel)config.logLevel;
  160. sdkConfig.logListener = ^(V2TIMLogLevel logLevel, NSString *logContent) {
  161. if (config.onLog) {
  162. config.onLog(logLevel, logContent);
  163. }
  164. };
  165. } else {
  166. sdkConfig.logLevel = V2TIM_LOG_INFO;
  167. }
  168. if ([[V2TIMManager sharedInstance] initSDK:sdkAppID config:sdkConfig]) {
  169. [NSNotificationCenter.defaultCenter postNotificationName:TUIInitSdkSuccessNotification object:nil];
  170. } else {
  171. [NSNotificationCenter.defaultCenter postNotificationName:TUIInitSdkFailNotification object:nil];
  172. }
  173. [V2TIMManager.sharedInstance addIMSDKListener:self];
  174. [[V2TIMManager sharedInstance] callExperimentalAPI:@"getLoginAccountType" param:nil succ:^(NSObject *result) {
  175. int accountType = [((NSNumber *)result) intValue];
  176. [TUILogin.shareInstance loginImpl:accountType userID:userID userSig:userSig config:config succ:succ fail:fail];
  177. } fail:^(int code, NSString *desc) {
  178. [TUILogin.shareInstance loginImpl:TUI_ACCOUNT_TYPE_UNKOWN userID:userID userSig:userSig config:config succ:succ fail:fail];
  179. }];
  180. }
  181. - (void)loginImpl:(int)loginAccountType userID:(NSString *)userID userSig:(NSString *)userSig
  182. config:(TUILoginConfig *)config succ:(TSucc)succ fail:(TFail)fail {
  183. if ([[[V2TIMManager sharedInstance] getLoginUser] isEqualToString:userID] && loginAccountType == TUI_ACCOUNT_TYPE_IM) {
  184. if (succ) {
  185. succ();
  186. }
  187. [NSNotificationCenter.defaultCenter postNotificationName:TUILoginSuccessNotification object:nil];
  188. return;
  189. }
  190. if (config && config.initLocalStorageOnly) {
  191. [[V2TIMManager sharedInstance] callExperimentalAPI:@"initLocalStorage" param:self.userID succ:^(NSObject *result) {
  192. if (succ) {
  193. succ();
  194. }
  195. } fail:^(int code, NSString *desc) {
  196. if (fail) {
  197. fail(code, desc);
  198. }
  199. }];
  200. return;
  201. }
  202. __weak __typeof(self) weakSelf = self;
  203. [[V2TIMManager sharedInstance] login:userID
  204. userSig:userSig
  205. succ:^{
  206. __strong __typeof(weakSelf) strongSelf = weakSelf;
  207. [strongSelf getSelfUserInfo];
  208. if (succ) {
  209. succ();
  210. }
  211. [NSNotificationCenter.defaultCenter postNotificationName:TUILoginSuccessNotification object:nil];
  212. }
  213. fail:^(int code, NSString *desc) {
  214. self.loginWithInit = NO;
  215. if (fail) {
  216. fail(code, desc);
  217. }
  218. [NSNotificationCenter.defaultCenter postNotificationName:TUILoginFailNotification object:nil];
  219. }];
  220. }
  221. - (void)getSelfUserInfo {
  222. if (self.userID == nil) {
  223. return;
  224. }
  225. __weak typeof(self) weakSelf = self;
  226. [[V2TIMManager sharedInstance] getUsersInfo:@[ self.userID ]
  227. succ:^(NSArray<V2TIMUserFullInfo *> *infoList) {
  228. V2TIMUserFullInfo *info = infoList.firstObject;
  229. weakSelf.nickName = info.nickName;
  230. weakSelf.faceUrl = info.faceURL;
  231. }
  232. fail:nil];
  233. }
  234. - (void)logout:(TSucc)succ fail:(TFail)fail {
  235. self.userID = @"";
  236. self.userSig = @"";
  237. self.currentBusinessScene = None;
  238. [[V2TIMManager sharedInstance]
  239. logout:^{
  240. if (succ) {
  241. succ();
  242. }
  243. if (self.loginWithInit) {
  244. // The new interface is currently used to log in. When logging out, you need to deinitialize and remove the listener.
  245. [V2TIMManager.sharedInstance removeIMSDKListener:self];
  246. [V2TIMManager.sharedInstance unInitSDK];
  247. self.sdkAppID = 0;
  248. }
  249. [NSNotificationCenter.defaultCenter postNotificationName:TUILogoutSuccessNotification object:nil];
  250. }
  251. fail:^(int code, NSString *desc) {
  252. if (fail) {
  253. fail(code, desc);
  254. }
  255. [NSNotificationCenter.defaultCenter postNotificationName:TUILogoutFailNotification object:nil];
  256. }];
  257. }
  258. - (void)addLoginListener:(id<TUILoginListener>)listener {
  259. if (listener == nil) {
  260. return;
  261. }
  262. @synchronized(self) {
  263. if (![_loginListenerSet.allObjects containsObject:listener]) {
  264. [_loginListenerSet addObject:listener];
  265. }
  266. }
  267. }
  268. - (void)removeLoginListener:(id<TUILoginListener>)listener {
  269. if (listener == nil) {
  270. return;
  271. }
  272. @synchronized(self) {
  273. if ([_loginListenerSet.allObjects containsObject:listener]) {
  274. [_loginListenerSet removeObject:listener];
  275. }
  276. }
  277. }
  278. - (int)getSdkAppID {
  279. return self.sdkAppID;
  280. }
  281. - (NSString *)getUserID {
  282. return self.userID;
  283. }
  284. - (NSString *)getUserSig {
  285. return self.userSig;
  286. }
  287. - (NSString *)getNickName {
  288. return self.nickName;
  289. }
  290. - (NSString *)getFaceUrl {
  291. return self.faceUrl;
  292. }
  293. - (void)doInMainThread:(dispatch_block_t)callback {
  294. if ([NSThread isMainThread]) {
  295. if (callback) {
  296. callback();
  297. }
  298. return;
  299. }
  300. dispatch_async(dispatch_get_main_queue(), callback);
  301. }
  302. #pragma mark - V2TIMSDKListener
  303. - (void)onConnecting {
  304. __weak typeof(self) weakSelf = self;
  305. [self doInMainThread:^{
  306. for (id<TUILoginListener> listener in weakSelf.loginListenerSet) {
  307. if ([listener respondsToSelector:@selector(onConnecting)]) {
  308. [listener onConnecting];
  309. }
  310. }
  311. [TUICore notifyEvent:TUICore_NetworkConnection_EVENT_CONNECTION_STATE_CHANGED
  312. subKey:TUICore_NetworkConnection_EVENT_SUB_KEY_CONNECTING
  313. object:nil
  314. param:nil];
  315. }];
  316. }
  317. - (void)onConnectSuccess {
  318. __weak typeof(self) weakSelf = self;
  319. [self doInMainThread:^{
  320. for (id<TUILoginListener> listener in weakSelf.loginListenerSet) {
  321. if ([listener respondsToSelector:@selector(onConnectSuccess)]) {
  322. [listener onConnectSuccess];
  323. }
  324. }
  325. [TUICore notifyEvent:TUICore_NetworkConnection_EVENT_CONNECTION_STATE_CHANGED
  326. subKey:TUICore_NetworkConnection_EVENT_SUB_KEY_CONNECT_SUCCESS
  327. object:nil
  328. param:nil];
  329. }];
  330. }
  331. - (void)onConnectFailed:(int)code err:(NSString *)err {
  332. __weak typeof(self) weakSelf = self;
  333. [self doInMainThread:^{
  334. for (id<TUILoginListener> listener in weakSelf.loginListenerSet) {
  335. if ([listener respondsToSelector:@selector(onConnectFailed:err:)]) {
  336. [listener onConnectFailed:code err:err];
  337. }
  338. }
  339. [TUICore notifyEvent:TUICore_NetworkConnection_EVENT_CONNECTION_STATE_CHANGED
  340. subKey:TUICore_NetworkConnection_EVENT_SUB_KEY_CONNECT_FAILED
  341. object:nil
  342. param:nil];
  343. }];
  344. }
  345. - (void)onKickedOffline {
  346. self.currentBusinessScene = None;
  347. __weak typeof(self) weakSelf = self;
  348. [self doInMainThread:^{
  349. for (id<TUILoginListener> listener in weakSelf.loginListenerSet) {
  350. if ([listener respondsToSelector:@selector(onKickedOffline)]) {
  351. [listener onKickedOffline];
  352. }
  353. }
  354. }];
  355. }
  356. - (void)onUserSigExpired {
  357. self.currentBusinessScene = None;
  358. __weak typeof(self) weakSelf = self;
  359. [self doInMainThread:^{
  360. for (id<TUILoginListener> listener in weakSelf.loginListenerSet) {
  361. if ([listener respondsToSelector:@selector(onUserSigExpired)]) {
  362. [listener onUserSigExpired];
  363. }
  364. }
  365. }];
  366. }
  367. - (void)onSelfInfoUpdated:(V2TIMUserFullInfo *)info {
  368. if (self.userID && self.userID.length > 0 && [self.userID isEqualToString:info.userID]) {
  369. self.nickName = info.nickName;
  370. self.faceUrl = info.faceURL;
  371. }
  372. }
  373. @end