// // AppSetup.m // AppScaffold // // Created by 谢培艺 on 2020/3/14. // Copyright © 2020 ko1o. All rights reserved. // // App 初始化设置 #import "AppSetup.h" #import "AppDelegate.h" #import #import "MTCommonListTable.h" #import #import "MTCacheManager.h" #import #import "MsgListViewController.h" #import "LaunchViewController.h" #import #import #import #import #import //#import //#import "ProfileViewController.h" #import "BottleViewController.h" #import #import #import "AlertInputView.h" #import "BottleNerbyViewController.h" #import "BottleBaseViewController.h" #import "MineViewController.h" #import "InviteCodeViewController.h" #import "PhoneLoginViewController.h" #import "THTabBarViewController.h" //#import "THNavigationController.h" #import "THHomeViewController.h" #import "THLanternAnimationController.h" #import "THNearAndLanternViewController.h" #import "THDemoMineViewController.h" @implementation AppSetup + (void)initSetupWithWindow:(UIWindow *)window { if (!window) { window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [(AppDelegate *)[UIApplication sharedApplication].delegate setWindow:window]; } // 初始化键盘 [self setupIQKeyboardManager]; // 初始化hud [self setupSVProgressHUD]; // 初始化MMKV [self setupMMKV]; // 初始化下拉刷新 [self setupMJRefresh]; // 初始化通用列表 [self setupMTCommonListTab]; // 初始化IM UI [self setupIMUI]; /// 初始化toast [self setupToast]; // 初始化UM [self setupUM]; //在register之前打开log, 后续可以根据log排查问题 [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) { NSLog(@"WeChatSDK: %@", log); }]; // // //向微信注册 // [WXApi registerApp:WEIXIN_APP_ID // universalLink:UNIVERSAL_LINK]; //// //调用自检函数 // [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) { // NSLog(@"%@, %u, %@, %@", @(step), result.success, result.errorInfo, result.suggestion); // }]; // 初始化window [self setupWindowRootVCWithWindow:window isLanuch:YES]; // 窗口可见 [window makeKeyAndVisible]; } #pragma mark - 各个初始化设置 + (void)setupToast { CSToastStyle *style = [CSToastManager sharedStyle]; style.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.7]; style.messageColor = style.titleColor = COLOR_WITH_RGB(0x040000); [CSToastManager setSharedStyle:style]; } + (void)setupUM { // // 初始化UM App Key [UMConfigure initWithAppkey:UM_SHARE_APP_KEY channel:@"App Store"]; // // // 微信、QQ、微博完整版会校验合法的universalLink,不设置会在初始化平台失败 // //配置微信Universal Link需注意 universalLinkDic的key是rawInt类型,不是枚举类型 ,即为 UMSocialPlatformType.wechatSession.rawInt // [UMSocialGlobal shareInstance].universalLinkDic =@{ // @(UMSocialPlatformType_WechatSession): UNIVERSAL_LINK, // @(UMSocialPlatformType_QQ): UNIVERSAL_LINK, // }; // // /* 设置微信的appKey和appSecret */ // [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WEIXIN_APP_ID appSecret:WEIXIN_APP_SECRET redirectURL:UM_SHARE_REDIRECT_URL]; // // /* 设置分享到QQ互联的appID, // * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。 // */ // [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQ_APP_ID/*设置QQ平台的appID*/ appSecret:QQ_APP_SECRET redirectURL:UM_SHARE_REDIRECT_URL]; } + (void)setupIMUI { TUIConfig *config = [TUIConfig defaultConfig]; config.avatarType = TAvatarTypeRadiusCorner; config.avatarCornerRadius = 8; config.defaultAvatarImage = [UIImage imageNamed:@"TH_defalut_avatar"]; TUIMessageCellLayout *outgoingTextMessageLayout = [TUIMessageCellLayout outgoingTextMessageLayout]; outgoingTextMessageLayout.avatarSize = CGSizeMake(FIX_SIZE(40), FIX_SIZE(40)); UIEdgeInsets insets = outgoingTextMessageLayout.avatarInsets; outgoingTextMessageLayout.avatarInsets = UIEdgeInsetsMake(0, FIX_SIZE(5.5), 0, FIX_SIZE(15)); TUIMessageCellLayout *outgoingVoiceMessageLayout = [TUIMessageCellLayout outgoingVoiceMessageLayout]; outgoingVoiceMessageLayout.avatarSize = outgoingTextMessageLayout.avatarSize; outgoingVoiceMessageLayout.avatarInsets = outgoingTextMessageLayout.avatarInsets; TUIMessageCellLayout *outgoingMessageLayout = [TUIMessageCellLayout outgoingMessageLayout]; outgoingMessageLayout.avatarSize = outgoingTextMessageLayout.avatarSize; outgoingMessageLayout.avatarInsets = outgoingTextMessageLayout.avatarInsets; TUIMessageCellLayout *incommingTextMessageLayout = [TUIMessageCellLayout incommingTextMessageLayout]; incommingTextMessageLayout.avatarSize = outgoingTextMessageLayout.avatarSize; incommingTextMessageLayout.avatarInsets = UIEdgeInsetsMake(insets.top, insets.right, insets.bottom, insets.left); TUIMessageCellLayout *incommingVoiceMessageLayout = [TUIMessageCellLayout incommingVoiceMessageLayout]; incommingVoiceMessageLayout.avatarSize = incommingTextMessageLayout.avatarSize; incommingVoiceMessageLayout.avatarInsets = incommingTextMessageLayout.avatarInsets; TUIMessageCellLayout *incommingMessageLayout = [TUIMessageCellLayout incommingMessageLayout]; incommingMessageLayout.avatarSize = incommingTextMessageLayout.avatarSize; incommingMessageLayout.avatarInsets = incommingTextMessageLayout.avatarInsets; // 设置发送文字消息的字体和颜色;设置接收的方法类似 [TUITextMessageCellData setOutgoingTextFont:NORMAL_FONT]; [TUITextMessageCellData setOutgoingTextColor:COLOR_WITH_RGB(0x040000)]; [TUITextMessageCellData setIncommingTextFont:NORMAL_FONT]; [TUITextMessageCellData setIncommingTextColor:UIColor.whiteColor]; } + (void)setupWindowRootVCWithWindow:(UIWindow *)window isLanuch:(BOOL)isLanuch { // window.rootViewController = [[PYNavigationViewController alloc] initWithRootViewController:[NSClassFromString(@"SetNickNameViewController") new]]; // return; if (![LoginService token] || ![LoginService isUserSigVaild]) { // 无Token 或者 UserSig 过期 触发登录 //附近-筛选参数. [[NSUserDefaults standardUserDefaults]setObject:@{} forKey:@"UserNearbyShaiXuanParamDict"]; NSDictionary* UserNearbyShaiXuanDict = @{@"sx_age":@"全部",@"sx_gender":@"全部",@"sx_gjsx":@"全部"}; [[NSUserDefaults standardUserDefaults]setObject:UserNearbyShaiXuanDict forKey:@"UserNearbyShaiXuanDict"]; window.rootViewController = [[PYNavigationViewController alloc] initWithRootViewController:[[PhoneLoginViewController alloc] init]]; // LaunchViewController return; } if (isLanuch == YES && [LoginService mainToken] != nil && [UserService currentUser].vip_kind <= 0 && [LoginService isClone] > 0) { //之前登录的是分身,切成主号的token [LoginService cacheToken:[LoginService mainToken]]; } if (![LoginService isTokenVaild]) { // token过期 刷新token [LoginService refreshTokenWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) { if (error) { [PYAppService logout]; }else{ [AppSetup autoLogin:window isLanuch:isLanuch]; } }]; }else{ [AppSetup autoLogin:window isLanuch:isLanuch]; } } + (void)autoLogin:(UIWindow *)window isLanuch:(BOOL)isLanuch { // 登录成功 登录IM [TUILogin login:TIM_APPID userID:[LoginService imUserId] userSig:[LoginService userSig] succ:^{ NSLog(@"-----> 登录成功%@",[LoginService imUserId]); } fail:^(int code, NSString *msg) { [PYAppService logout]; [SVProgressHUD showErrorWithStatus:@"IM登录失败"]; }]; //正常登录,更新基础信息 [UserService getUserInfoWithCompletion:^(User * _Nonnull user) { //更新会员信息 [UserService loadUserOtherInfosWithUserIDs:@[@(user.ID).stringValue] userCache:YES comoletion:^(id _Nullable rsp, NSError * _Nullable error) { }]; }]; // 初始化tab [self setupTabViewControllerWithWindow:window]; } + (void)setupTabViewControllerWithWindow:(UIWindow *)window{ NSArray *childControllers = @[ // PYNavigationViewController // [[PYNavigationViewController alloc] initWithRootViewController:[BottleViewController new]], // [[THNavigationController alloc] initWithRootViewController:[THLanternAnimationController new]], // [[THNavigationController alloc] initWithRootViewController:[THHomeViewController new]], [[PYNavigationViewController alloc] initWithRootViewController:[MsgListViewController new]], [[PYNavigationViewController alloc] initWithRootViewController:[THNearAndLanternViewController new]], // [[PYNavigationViewController alloc] initWithRootViewController:[BottleNerbyViewController new]], // [[PYNavigationViewController alloc] initWithRootViewController:[InviteCodeViewController new]], // [[PYNavigationViewController alloc] initWithRootViewController:[MsgListViewController new]], // [[PYNavigationViewController alloc] initWithRootViewController:[THDemoMineViewController new]], [[PYNavigationViewController alloc] initWithRootViewController:[MineViewController new]], ]; NSArray *titles = @[ @"消息", @"基地", @"我的", ]; NSArray *iconNames = @[ @"TH_TabBarMsgNormal", @"TH_TabBarBaseLocalNormal", @"TH_TabBarMineNormal" ]; NSArray *iconSelectedNames = @[ @"TH_TabBarMsgSelected", @"TH_TabBarBaseLocalSelected", @"TH_TabBarMineSelected" ]; NSMutableArray *attrs = [NSMutableArray array]; for (int i = 0; i < titles.count; i++) { NSMutableDictionary *attrM = [NSMutableDictionary dictionary]; attrM[CYLTabBarItemTitle] = titles[i]; attrM[CYLTabBarItemImage] = [[UIImage imageNamed:iconNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; attrM[CYLTabBarItemSelectedImage] = [[UIImage imageNamed:iconSelectedNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [attrs addObject:attrM]; } /* THTabBarViewController *tabBarView = [[THTabBarViewController alloc] init]; for (UINavigationController *nav in childControllers) { [tabBarView addChildViewController:nav]; } window.rootViewController = tabBarView; */ CYLTabBarController *tabVC = [CYLTabBarController tabBarControllerWithViewControllers:childControllers tabBarItemsAttributes:attrs imageInsets:UIEdgeInsetsZero titlePositionAdjustment:UIOffsetMake(0, HOME_INDICATOR_HEIHT > 0 ? 3 : 0)]; tabVC.tabBar.tintColor = THEME_COLOR; tabVC.tabBar.unselectedItemTintColor = [UIColor whiteColor]; tabVC.tabBar.translucent = NO; // tabVC.tabBar.backgroundColor = [COLOR_WITH_RGB(0x151032) colorWithAlphaComponent:0.8]; tabVC.tabBar.backgroundColor = [COLOR_WITH_RGB(0x000000) colorWithAlphaComponent:1]; UIView *lineView = [UIView lineViewWithWidth:SCREEN_WIDTH]; lineView.height = 0.5; [tabVC.tabBar addSubview:lineView]; [tabVC hideTabBadgeBackgroundSeparator]; BOOL isFromChangeAccount = [window.rootViewController isKindOfClass:CYLTabBarController.class]; window.rootViewController = tabVC; // 异步获取app配置 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // app更新提示 [PYHTTPManager postWithPath:@"checkappupdate" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { NSString *type = rsp[@"update_type"]; NSString *version = rsp[@"new_version"]; NSString *content = rsp[@"update_content"]; NSString *app_urlStr = rsp[@"app_url"]; // NSLog(@"type:%@\napp_urlStr:%@",type,app_urlStr); // NSString *type = @"force_update"; // NSString *content = @"https://sourl.cn/9KSTN6"; // NSString *app_urlStr = @"https://sourl.cn/9KSTN6"; if ([type isEqualToString:@"update_remind"]) { // 提醒更新 [self showAppUpdateAlertViewWithVersion:version content:content isForce:NO andDownLoadAddress:app_urlStr]; return; } if ([type isEqualToString:@"force_update"]) { // 强制更新 [self showAppUpdateAlertViewWithVersion:version content:content isForce:YES andDownLoadAddress:app_urlStr]; return; } } }]; }); }); // window.rootViewController = [[PYNavigationViewController alloc] initWithRootViewController:[NSClassFromString(@"VIPViewController") new]]; dispatch_async(dispatch_get_main_queue(), ^{ for (UIView *subView in tabVC.tabBar.subviews) { if ([NSStringFromClass([subView class]) isEqualToString:@"_UIBarBackground"]) { subView.hidden = YES; } } if (!isFromChangeAccount) { [tabVC setSelectedIndex:0]; // 默认选中瓶子Tab // is_security_on if ([UserService isSecurityCodeOpen]) { // NSLog(@" [UserService currentUser].security_code : %@",[UserService currentUser].security_code); [AlertInputView showCheckPasswordWithTitle:@"验证密码锁" content:nil maxLength:SecurityCodeLength action:^BOOL(NSString * _Nonnull content) { if (![content isEqualToString:[UserService currentUser].security_code]) { [ToastUtil showToast:@"密码验证失败" position:CSToastPositionTop]; return NO; } return YES; }]; } } }); // tabVC.selectedIndex = 1; } + (void)showAppUpdateAlertViewWithVersion:(NSString *)version content:(NSString *)content isForce:(BOOL)isForce andDownLoadAddress:(NSString *)app_urlStr{ UIView *customView = [[UIView alloc] init]; customView.width = FIX_SIZE(270); customView.height = FIX_SIZE(494); customView.backgroundColor = COLOR_WITH_RGB(0x222436); customView.layer.cornerRadius = FIX_SIZE(10); customView.clipsToBounds = YES; MTAlertView *alerView = [MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) { config.customView = customView; }]; UIImageView *iconView = [[UIImageView alloc] init]; iconView.width = customView.width; iconView.height = FIX_SIZE(160); iconView.image = ImageNamed(@"TH_app_update_img"); [customView addSubview:iconView]; UILabel *versionLabel = [[UILabel alloc] init]; versionLabel.text = version; versionLabel.font = MT_FONT_REGULAR_SIZE(12); versionLabel.textColor = [UIColor whiteColor]; [versionLabel sizeToFit]; versionLabel.x = FIX_SIZE(25); versionLabel.centerY = iconView.height * 0.5; [iconView addSubview:versionLabel]; UIButton *nextButton = [[UIButton alloc] init]; [nextButton setTitle:@"稍后更新" forState:UIControlStateNormal]; [nextButton setTitleColor:CONTENT_COLOR forState:UIControlStateNormal]; [nextButton sizeToFit]; nextButton.width += 10; nextButton.height = FIX_SIZE(30); nextButton.titleLabel.font = MT_FONT_REGULAR_SIZE(13); nextButton.centerX = customView.width * 0.5; nextButton.bottom = customView.height - FIX_SIZE(15); nextButton.hidden = isForce; [customView addSubview:nextButton]; PYThemeButton *button = [PYThemeButton buttonWithTitle:@"立即更新" action:^(UIButton * _Nonnull button) { // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/qq/%@?mt=8", APPSTORE_APPID]] options:@{} completionHandler:^(BOOL success) { //// NSLog(@"to App Store"); // }]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:app_urlStr] options:@{} completionHandler:^(BOOL success) { // NSLog(@"to App Store"); }]; }]; [nextButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) { [alerView dismiss]; }]; button.width = FIX_SIZE(180); button.centerX = customView.width * 0.5; button.bottom = isForce ? customView.height - FIX_SIZE(20) : nextButton.y - FIX_SIZE(8); [customView addSubview:button]; UITextView *textView = [[UITextView alloc] init]; textView.x = FIX_SIZE(20); textView.text = content; textView.backgroundColor = [UIColor clearColor]; textView.width = customView.width - textView.x * 2; textView.y = iconView.bottom + FIX_SIZE(20); textView.height = button.y - FIX_SIZE(20) - textView.y; textView.textColor = [UIColor whiteColor]; textView.font = MT_FONT_REGULAR_SIZE(15); [customView addSubview:textView]; } + (void)setupIQKeyboardManager { IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; keyboardManager.toolbarDoneBarButtonItemText = @"完成"; keyboardManager.toolbarNextBarButtonItemText = nil; keyboardManager.toolbarPreviousBarButtonItemText = nil; keyboardManager.previousNextDisplayMode = IQPreviousNextDisplayModeAlwaysHide; } + (void)setupSVProgressHUD { // 不允许用户交互 [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear]; [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom]; [SVProgressHUD setMinimumSize:CGSizeMake(FIX_SIZE(360 * 0.5), FIX_SIZE(300 * 0.5))]; [SVProgressHUD setFont:MT_FONT_REGULAR_SIZE(16)]; [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; [SVProgressHUD setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]]; [SVProgressHUD setCornerRadius:6]; [SVProgressHUD setImageViewSize:CGSizeMake(FIX_SIZE(90 * 0.5), FIX_SIZE(90 * 130 / 120.0 * 0.5))]; [SVProgressHUD setErrorImage:ImageNamed(@"TH_hud_error_icon")]; [SVProgressHUD setSuccessImage:ImageNamed(@"TH_hud_succeed_icon")]; [SVProgressHUD setInfoImage:ImageNamed(@"TH_hud_info_icon")]; [SVProgressHUD setMinimumDismissTimeInterval:1.0]; [SVProgressHUD setMaximumDismissTimeInterval:2.0]; [SVProgressHUD setMaxSupportedWindowLevel:CGFLOAT_MAX]; } + (void)setupMMKV { // 这里返回用户uid [MTCacheManager setupCacheManager:^NSString * _Nullable{ return @([UserService currentUserID]).stringValue; }]; } + (void)setupMJRefresh { } + (void)setupMTCommonListTab { MTCommonListTableConfig *tableConfig = [MTCommonListTableConfig sharedConfig]; tableConfig.tableBackgroundColor = BG_COLOR; tableConfig.cellBackgroudColor = [UIColor clearColor]; tableConfig.cellTitleFont = MT_FONT_MEDIUM_SIZE(14); tableConfig.cellImageTintColor = THEME_COLOR; tableConfig.cellTitleColor = COLOR_WITH_RGB(0xFFFFFF); tableConfig.cellImageSize = CGSizeMake(FIX_SIZE(26), FIX_SIZE(26)); tableConfig.cellImageMarginLeft = FIX_SIZE(8); tableConfig.cellAccessoryDisclosureIndicatorImageName = @"TH_icon_right_black"; tableConfig.cellAccessoryImageSize = CGSizeMake(FIX_SIZE(15), FIX_SIZE(15)); tableConfig.sectionHeaderHeight = FIX_SIZE(10); tableConfig.cellSeparatorLeadingType = MTCommonListTableCellSeparatorLeadingTypeLeadingTitle; tableConfig.separatorStyle = UITableViewCellSeparatorStyleSingleLine; tableConfig.cellAccessoryTitleFont = MT_FONT_REGULAR_SIZE(11); tableConfig.cellAccessoryTitleColor = COLOR_WITH_RGB(0xB3B3B3); tableConfig.cellHeight = FIX_SIZE(48); tableConfig.cellImageMarginLeft = FIX_SIZE(15.5); } @end