// // UITabBarViewController.m // myWatch // // Created by mobao on 15/10/9. // Copyright (c) 2015年 xTT. All rights reserved. // #import "MyTabBarViewController.h" #import "AppDelegate.h" #import "myHelper.h" #import "MyMQTT.h" #import "User.h" #import "JCManager.h" #import "WatchHomeViewController.h" #import "MoreViewController.h" #import "DiscoverViewController.h" #import "DeviceTabViewController.h" #import "MapViewController.h" #import "LoginViewController.h" #import "PhoneLoginViewController.h" #import "ForgetPwdViewController.h" #import "RegisteredViewController.h" @interface MyTabBarViewController (){ UIView *launchView; NSString *LaunchImageName; } //@property (nonatomic ,strong)WCADView *adView ; @property(nonatomic, strong) UIImageView *launchImageView; @property (nonatomic,strong) UIImageView *bottomView; @property (nonatomic,strong) UIWindow *subWin; //@property (nonatomic,strong) GDTSplashAd *splash; @property (nonatomic,strong) UIImageView *TransitionImageView; @end @implementation MyTabBarViewController - (void)viewDidLoad { [super viewDidLoad]; if (@available(iOS 15.0, *)) { UITabBarAppearance *barAppearance = [[UITabBarAppearance alloc] init]; barAppearance.backgroundEffect = nil; self.tabBar.scrollEdgeAppearance = barAppearance; self.tabBar.standardAppearance = barAppearance; } else { // Fallback on earlier versions [UITabBar appearance].translucent = NO; } self.delegate = self; [self addChildViewControllers]; [self addTabarItems]; [self addNotification]; if(!cUser)return; CGSize viewSize = self.view.bounds.size; NSString *viewOrientation = @"Portrait"; //横屏请设置成 @"Landscape" NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"]; for (NSDictionary* dict in imagesDict) { CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]); if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) { LaunchImageName = dict[@"UILaunchImageName"]; } } if(!LaunchImageName) { if(ScreenHeight >= 812){ LaunchImageName = @"启动图X"; }else{ LaunchImageName = @"启动图"; } } //MARK: 有时会在首页显示之后再显示广告, 就是闪现首页,所以这里加个 View 做下过渡 会在首页加载完成数据后删除 _TransitionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]; _TransitionImageView.image = [UIImage imageNamed:LaunchImageName]; //[self.view addSubview: _TransitionImageView]; } - (void)addCustomChildVC:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName { vc.tabBarItem.title = title; vc.tabBarItem.image = [UIImage imageNamed:imageName]; vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; ZXNavigationBarNavigationController *nav = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:vc]; [self addChildViewController:nav]; } - (void)addChildViewControllers { UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *homeVC = [sb instantiateViewControllerWithIdentifier:@"WatchHomeViewController"]; ZXNavigationBarNavigationController *homeNav = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:homeVC]; ZXNavigationBarNavigationController *devNav = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:[DeviceTabViewController new]]; MapViewController *mapVc = [sb instantiateViewControllerWithIdentifier:@"MapViewController"]; ZXNavigationBarNavigationController *mapNav = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:mapVc]; ZXNavigationBarNavigationController *myNav = [[ZXNavigationBarNavigationController alloc] initWithRootViewController:[MoreViewController new]]; self.viewControllers = @[homeNav, devNav, mapNav, myNav]; self.tabBar.tintColor = mainColor; } - (void)addTabarItems { NSArray *titleNames = @[@"首页", @"设备", @"定位", @"我的"]; NSArray *normalImages = @[@"icon_home", @"icon_device", @"icon_location", @"icon_my"]; NSArray *selectedImages = @[@"tabBar_home_selected", @"icon_device_select", @"icon_location_select", @"icon_my_select"]; //SNJJSELF [self.childViewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop) { obj.tabBarItem.title = [titleNames objectAtIndex:idx]; obj.tabBarItem.image = [[UIImage imageNamed:normalImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; obj.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImages[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; }]; if (@available(iOS 13.0, *)) { self.tabBar.unselectedItemTintColor = KKTextColor; self.tabBar.tintColor = mainColor; self.tabBar.backgroundColor = KKWhiteColorColor; } else { self.tabBar.backgroundColor = KKWhiteColorColor; [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: KKTextColor} forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: KKWhiteColorColor} forState:UIControlStateSelected]; } } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { } -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { UINavigationController *selectNav = (UINavigationController*)viewController; if (cUser.myDevices.count == 0 && [NSStringFromClass([selectNav.topViewController class]) isEqualToString:@"DeviceTabViewController"]) { //要跳 定位页面 WEAKSELF weakSelf.selectedIndex = 0; LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"绑定设备" message:@"未绑定设备,是否前往绑定?" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { // add by lsz 2020-07-13 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil]; UINavigationController *vc = [sb instantiateViewControllerWithIdentifier:@"ScanCodeViewController"]; UINavigationController *homeNav = (UINavigationController*)weakSelf.viewControllers.firstObject; //获取 首页的 UINavigationController [homeNav pushViewController:vc animated:YES]; } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; return NO; } return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)removeTransitionImageView { if(_TransitionImageView){ // xLog(@"开始-0----------"); [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self->_TransitionImageView.alpha = 0.0f; } completion:^(BOOL finished) { if(finished){ // xLog(@"结束-----------finished:%d",finished); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self->_TransitionImageView removeFromSuperview]; self->_TransitionImageView = nil; }); } }]; } } - (void)addNotification { [[NSNotificationCenter defaultCenter] addObserverForName:@"outLogin" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { if(!cUser){ //如果 outLogin 推送前 用户信息已经没有了,那么就是 token无效的场景,清除密码 [UserDefaults setBool:NO forKey:kifRememberPWDKey]; [UserDefaults removeObjectForKey:@"lastpwd"]; } //清空推送角标 [UIApplication sharedApplication].applicationIconBadgeNumber = 0; [User setCurrentUser:nil]; //如果有菊风设备的,退出菊风SDK的推送 [[JCManager shared] uninitialize]; //退出登录,清除每天第一次登陆的记录 [UserDefaults removeObjectForKey:FirstEveryDayKey]; [self goLoginVCWithAnimate:YES]; if (self->launchView) { [UIView animateWithDuration:1.0f animations:^{ self->launchView.alpha = 0.0f; } completion:^(BOOL finished) { [self->launchView removeFromSuperview]; self->launchView = nil; }]; } }]; [[NSNotificationCenter defaultCenter] addObserverForName:PUSH_type_chatGroupDisband object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { if (!cUser) { return; } if ([cUser.openid isEqualToString:note.object[@"operator"]]) { [SVProgressHUD showSuccessWithStatus:@"该设备已经恢复出厂设置"]; [cUser localDeleteDevice]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"lastLoginName"]; [[NSUserDefaults standardUserDefaults] synchronize]; sleep(1); [self goLoginVCWithAnimate:YES]; }else{ //如果是关注的设备,被管理员 恢复出厂设置了,需要清空一下聊天群,然后回到主页面重新 刷新数据 //清空 一下 聊天群组 cUser.chatGroups = nil; [self setSelectedIndex:0]; UINavigationController *navVC = (UINavigationController*)self.selectedViewController; [navVC popToRootViewControllerAnimated:YES]; } }]; } - (void)goLoginVCWithAnimate:(BOOL)animate { //清空通知栏消息 //要先把BadgeNumber 设成跟当前不同的值,然后再设成0,就欧了; [UIApplication sharedApplication].applicationIconBadgeNumber = 1; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; if(![[self getCurrentVC] isKindOfClass:[LoginViewController class]] && ![[self getCurrentVC] isKindOfClass:[PhoneLoginViewController class]] && ![[self getCurrentVC] isKindOfClass:[ForgetPwdViewController class]] && ![[self getCurrentVC] isKindOfClass:[RegisteredViewController class]]) { AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate; [app showRootViewController]; } // UINavigationController *vc = [addDeviceSB instantiateViewControllerWithIdentifier:@"LoginNav"]; // vc.modalPresentationStyle = UIModalPresentationFullScreen; // [self presentViewController:vc animated:animate completion:^{ // [self.viewControllers enumerateObjectsUsingBlock:^(__kindof UINavigationController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // UIViewController *vc = [obj.viewControllers firstObject]; // [vc.navigationController popToRootViewControllerAnimated:YES]; // }]; // // if (self.selectedIndex != 0) { // self.selectedIndex = 0; // } // }]; } //获取当前屏幕显示的viewcontroller - (UIViewController *)getCurrentVC { UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; UIViewController *currentVC = [self getCurrentVCFrom:rootViewController]; return currentVC; } - (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC { UIViewController *currentVC; if ([rootVC presentedViewController]) { // 视图是被presented出来的 rootVC = [rootVC presentedViewController]; } if ([rootVC isKindOfClass:[UITabBarController class]]) { // 根视图为UITabBarController currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]]; } else if ([rootVC isKindOfClass:[UINavigationController class]]) { // 根视图为UINavigationController currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]]; } else { // 根视图为非导航类 currentVC = rootVC; } return currentVC; } @end