| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- //
- // IMAPlatform.m
- // TIMAdapter
- //
- // Created by AlexiChen on 16/2/17.
- // Copyright © 2016年 AlexiChen. All rights reserved.
- //
- #import "IMAPlatform.h"
- @interface IMAPlatform ()<V2TIMSDKListener>
- @property (nonatomic, assign) TCQALNetwork networkType;
- @end
- @implementation IMAPlatform
- #define kIMAPlatformConfig @"IMAPlatformConfig"
- static IMAPlatform *_sharedInstance = nil;
- + (instancetype)configWith:(IMAPlatformConfig *)cfg
- {
- static dispatch_once_t predicate;
-
- dispatch_once(&predicate, ^{
- _sharedInstance = [[IMAPlatform alloc] init];
- [_sharedInstance configIMSDK:cfg];
- });
-
- return _sharedInstance;
-
- }
- static Class kHostClass = Nil;
- + (void)configHostClass:(Class)hostcls
- {
- if (![hostcls isSubclassOfClass:[IMAHost class]])
- {
- DebugLog(@"%@ 必须是IMAHost的子类型", hostcls);
- }
- else
- {
- kHostClass = hostcls;
- }
- }
- + (instancetype)sharedInstance
- {
- // TODO:
- return _sharedInstance;
- }
- #define kIMAPlatform_AutoLogin_Key @"kIMAPlatform_AutoLogin_Key"
- + (BOOL)isAutoLogin
- {
- NSNumber *num = [[NSUserDefaults standardUserDefaults] objectForKey:@"kIMAPlatform_AutoLogin_Key"];
- return [num boolValue];
- }
- + (void)setAutoLogin:(BOOL)autologin
- {
- [[NSUserDefaults standardUserDefaults] setObject:@(autologin) forKey:kIMAPlatform_AutoLogin_Key];
- }
- //
- //- (void)initialFriendProxy:(TIMSucc)succ fail:(TIMFail)fail
- //{
- // // 说明登录成功
- //// _friendShipMgr = [[TIMFriendshipManager sharedInstance] getFriendshipProxy];
- //// [_friendShipMgr SetProxyListener:self];
- //// [_friendShipMgr SyncWithFlags:0XFF custom:nil succ:succ fail:fail];
- //
- //
- //}
- - (IMAContactManager *)contactMgr
- {
- if (!_contactMgr)
- {
- _contactMgr = [[IMAContactManager alloc] init];
- }
- return _contactMgr;
- }
- - (IMAConversationManager *)conversationMgr
- {
- if (!_conversationMgr)
- {
- _conversationMgr = [[IMAConversationManager alloc] init];
- }
- return _conversationMgr;
- }
- - (IMAUser *)getReceiverOf:(IMAConversation *)conv
- {
- NSString *receiver = [conv receiver];
- if (conv.type == TIM_C2C)
- {
- return [self.contactMgr getUserByUserId:receiver];
- }
- else if (conv.type == TIM_GROUP)
- {
- // 查询群列表
- return [self.contactMgr getUserByGroupId:receiver];
- }
- else
- {
- DebugLog(@"不支持的会话模式");
- return nil;
- }
- }
- - (void)configIMSDK:(IMAPlatformConfig *)cfg
- {
- TIMManager *manager = [TIMManager sharedInstance];
-
- [manager setEnv:cfg.environment];
-
- TIMSdkConfig *config = [[TIMSdkConfig alloc] init];
- config.sdkAppId = [TXYSdkAppId intValue] ;
- config.accountType = TXYSdkAccountType;
- config.disableCrashReport = NO;
- config.connListener = self;
- config.logLevel = TIM_LOG_NONE;
- [manager initSdk:config];
-
- TIMUserConfig *userConfig = [[TIMUserConfig alloc] init];
- // userConfig.disableStorage = YES;//禁用本地存储(加载消息扩展包有效)
- // userConfig.disableAutoReport = YES;//禁止自动上报(加载消息扩展包有效)
- // userConfig.enableReadReceipt = YES;//开启C2C已读回执(加载消息扩展包有效)
- userConfig.disableRecnetContact = NO;//不开启最近联系人(加载消息扩展包有效)
- userConfig.disableRecentContactNotify = YES;//不通过onNewMessage:抛出最新联系人的最后一条消息(加载消息扩展包有效)
- userConfig.enableFriendshipProxy = YES;//开启关系链数据本地缓存功能(加载好友扩展包有效)
- userConfig.enableGroupAssistant = YES;//开启群组数据本地缓存功能(加载群组扩展包有效)
- TIMGroupInfoOption *giOption = [[TIMGroupInfoOption alloc] init];
- giOption.groupFlags = 0xffffff;//需要获取的群组信息标志(TIMGetGroupBaseInfoFlag),默认为0xffffff
- giOption.groupCustom = nil;//需要获取群组资料的自定义信息(NSString*)列表
- userConfig.groupInfoOpt = giOption;//设置默认拉取的群组资料
- TIMGroupMemberInfoOption *gmiOption = [[TIMGroupMemberInfoOption alloc] init];
- gmiOption.memberFlags = 0xffffff;//需要获取的群成员标志(TIMGetGroupMemInfoFlag),默认为0xffffff
- gmiOption.memberCustom = nil;//需要获取群成员资料的自定义信息(NSString*)列表
- userConfig.groupMemberInfoOpt = gmiOption;//设置默认拉取的群成员资料
- TIMFriendProfileOption *fpOption = [[TIMFriendProfileOption alloc] init];
- fpOption.friendFlags = 0xffffff;//需要获取的好友信息标志(TIMProfileFlag),默认为0xffffff
- fpOption.friendCustom = nil;//需要获取的好友自定义信息(NSString*)列表
- fpOption.userCustom = nil;//需要获取的用户自定义信息(NSString*)列表
- userConfig.friendProfileOpt = fpOption;//设置默认拉取的好友资料
- userConfig.userStatusListener = self;//用户登录状态监听器
- userConfig.refreshListener = self;//会话刷新监听器(未读计数、已读同步)(加载消息扩展包有效)
- // userConfig.receiptListener = self;//消息已读回执监听器(加载消息扩展包有效)
- // userConfig.messageUpdateListener = self;//消息svr重写监听器(加载消息扩展包有效)
- // userConfig.uploadProgressListener = self;//文件上传进度监听器
- // userConfig.groupEventListener todo
- userConfig.messgeRevokeListener = self.conversationMgr;
- userConfig.friendshipListener = self;//关系链数据本地缓存监听器(加载好友扩展包、enableFriendshipProxy有效)
- userConfig.groupListener = self;//群组据本地缓存监听器(加载群组扩展包、enableGroupAssistant有效)
- [manager setUserConfig:userConfig];
-
-
- // 1. 从 IM 控制台获取应用 SDKAppID,详情请参考 SDKAppID。
- // 2. 初始化 config 对象
- V2TIMSDKConfig *config2 = [[V2TIMSDKConfig alloc] init];
- // 3. 指定 log 输出级别,详情请参考 [SDKConfig](#SDKAppID)。
- config2.logLevel = V2TIM_LOG_INFO;
- // 4. 初始化 SDK 并设置 V2TIMSDKListener 的监听对象。
- // initSDK 后 SDK 会自动连接网络,网络连接状态可以在 V2TIMSDKListener 回调里面监听。
- // [[V2TIMManager sharedInstance] initSDK:TXYSdkAppId.intValue config:config2 listener:self];
-
- [[V2TIMManager sharedInstance] initSDK:TXYSdkAppId.intValue config:config2];
- [[V2TIMManager sharedInstance] addIMSDKListener:self];
-
- }
- // 5. 监听 V2TIMSDKListener 回调
- - (void)onConnecting {
- // 正在连接到腾讯云服务器
- }
- - (void)onConnectSuccess {
- // 已经成功连接到腾讯云服务器
- }
- - (void)onConnectFailed:(int)code err:(NSString*)err {
- // 连接腾讯云服务器失败
- }
- - (void)saveToLocal
- {
- // 保存上一次联系人列表状态
- [_contactMgr saveToLocal];
-
- // 保存Config状态
- }
- - (void)onLogoutCompletion
- {
- [self offlineLogin];
-
- self.offlineExitLivingBlock = nil;
-
- [IMAPlatform setAutoLogin:NO];
- _host = nil;
-
- #if kIsUseAVSDKAsLiveScene
- [TCAVSharedContext destroyContextCompletion:nil];
- #endif
-
- }
- - (void)offlineLogin
- {
- // 被踢下线,则清空单例中的数据,再登录后再重新创建
- [self saveToLocal];
-
- _contactMgr = nil;
- [[TIMManager sharedInstance] removeMessageListener:_conversationMgr];
- _conversationMgr = nil;
- }
- - (void)logout:(TIMLoginSucc)succ fail:(TIMFail)fail
- {
- __weak IMAPlatform *ws = self;
-
- [[TIMManager sharedInstance] logout:^{
- [ws onLogoutCompletion];
- if (succ)
- {
- succ();
- }
- } fail:^(int code, NSString *err) {
- [ws onLogoutCompletion];
- if (fail)
- {
- fail(code, err);
- }
- }];
- }
- - (IMAPlatformConfig *)localConfig
- {
- return _host.loginParm.config;
- }
- - (void)configHost:(TIMLoginParam *)param
- {
- if (!_host)
- {
- if (kHostClass == Nil)
- {
- kHostClass = [IMAHost class];
- }
- _host = [[kHostClass alloc] init];
- }
- _host.loginParm = param;
- [_host getMyInfo:nil];
- //[_host asyncProfile];
-
- #if kIsUseAVSDKAsLiveScene
- [TCAVSharedContext configWithStartedContext:_host completion:nil];
- #endif
- }
- - (void)changeToNetwork:(TCQALNetwork)work
- {
- if (work > EQALNetworkType_ReachableViaWWAN)
- {
- // 不处理这些
- work = EQALNetworkType_ReachableViaWWAN;
- }
- DebugLog(@"网络切换到(-1:未知 0:无网 1:wifi 2:移动网):%d", work);
- // if (work != _networkType)
- // {
- self.networkType = work;
-
- // }
- }
- // 判断是否主播
- + (BOOL)isHost:(NSString *)userId
- {
- if (![BGUtils isBlankString:userId])
- {
- if ([[[IMAPlatform sharedInstance].host imUserId] isEqualToString:userId])
- {
- return YES;
- }
- else
- {
- return NO;
- }
- }
- else
- {
- return NO;
- }
- }
- @end
|