BGBaseAppDelegate.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // BGBaseAppDelegate.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 3/6/14.
  6. // Copyright (c) 2014 CommonLibrary. All rights reserved.
  7. //
  8. #import "BGBaseAppDelegate.h"
  9. #import <objc/runtime.h>
  10. #import "PathUtility.h"
  11. #import "NetworkUtility.h"
  12. #import "NavigationViewController.h"
  13. @implementation BGBaseAppDelegate
  14. + (instancetype)sharedAppDelegate
  15. {
  16. return [UIApplication sharedApplication].delegate;
  17. }
  18. - (void)redirectConsoleLog:(NSString *)logFile
  19. {
  20. NSString *cachePath = [PathUtility getCachePath];
  21. NSString *logfilePath = [NSString stringWithFormat:@"%@/%@", cachePath, logFile];
  22. freopen([logfilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
  23. }
  24. // 配置App中的控件的默认属性
  25. - (void)configAppearance
  26. {
  27. // [[UINavigationBar appearance] setBarTintColor:kNavBarThemeColor];
  28. // [[UINavigationBar appearance] setTintColor:kWhiteColor];
  29. NSShadow *shadow = [[NSShadow alloc] init];
  30. shadow.shadowColor = kWhiteColor;
  31. shadow.shadowOffset = CGSizeMake(0, 0);
  32. [[UINavigationBar appearance] setTitleTextAttributes:@{
  33. NSForegroundColorAttributeName:kWhiteColor,
  34. NSShadowAttributeName:shadow,
  35. NSFontAttributeName:kAppLargeTextFont
  36. }];
  37. [[UILabel appearance] setBackgroundColor:kClearColor];
  38. [[UILabel appearance] setTextColor:kAppGrayColor1];
  39. [[UIButton appearance] setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  40. // [[UITableViewCell appearance] setBackgroundColor:kClearColor];
  41. //
  42. // [[UITableViewCell appearance] setTintColor:kNavBarThemeColor];
  43. }
  44. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  45. {
  46. [self configAppearance];
  47. // 日志重定向处理
  48. if ([self needRedirectConsole])
  49. {
  50. //实例化一个NSDateFormatter对象
  51. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  52. //设定时间格式,这里可以设置成自己需要的格式
  53. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  54. //用[NSDate date]可以获取系统当前时间
  55. NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
  56. [self redirectConsoleLog:[NSString stringWithFormat:@"%@.log", currentDateStr]];
  57. }
  58. // 用StoryBoard不需要自己创建
  59. _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  60. _window.backgroundColor = [UIColor whiteColor];
  61. [self configAppLaunch];
  62. // 进入登录界面
  63. [self enterLoginUI];
  64. [_window makeKeyAndVisible];
  65. return YES;
  66. }
  67. - (void)configAppLaunch
  68. {
  69. // 作App配置
  70. [[NetworkUtility sharedNetworkUtility] startCheckWifi];
  71. }
  72. - (void)enterLoginUI
  73. {
  74. // 未提过前面的过渡界面,暂时先这样处理
  75. // 进入登录界面
  76. }
  77. - (BOOL)needRedirectConsole
  78. {
  79. return NO;
  80. }
  81. - (void)enterMainUI
  82. {
  83. NSLog(@"==");
  84. }
  85. // 获取当前活动的navigationcontroller
  86. - (UINavigationController *)navigationViewController
  87. {
  88. UIWindow *window = self.window;
  89. if ([window.rootViewController isKindOfClass:[UINavigationController class]])
  90. {
  91. UINavigationController *nav = (UINavigationController *)window.rootViewController;
  92. if (@available(iOS 15.0, *)) {
  93. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  94. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
  95. appearance.backgroundColor = kWhiteColor;
  96. nav.navigationBar.scrollEdgeAppearance = appearance;
  97. }
  98. return nav;
  99. }
  100. else if ([window.rootViewController isKindOfClass:[UITabBarController class]])
  101. {
  102. UIViewController *selectVc = [((UITabBarController *)window.rootViewController) selectedViewController];
  103. if ([selectVc isKindOfClass:[UINavigationController class]])
  104. {
  105. UINavigationController *nav = (UINavigationController *)selectVc;
  106. if (@available(iOS 15.0, *)) {
  107. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  108. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
  109. appearance.backgroundColor = kWhiteColor;
  110. nav.navigationBar.scrollEdgeAppearance = appearance;
  111. }
  112. return nav;
  113. }
  114. }
  115. return nil;
  116. }
  117. - (UIViewController *)topViewController
  118. {
  119. UINavigationController *nav = [self navigationViewController];
  120. return nav.topViewController;
  121. }
  122. - (UIViewController *)theTopviewControler{
  123. //获取根控制器
  124. UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
  125. UIViewController *parent = rootVC;
  126. //遍历 如果是presentViewController
  127. while ((parent = rootVC.presentedViewController) != nil ) {
  128. rootVC = parent;
  129. }
  130. while ([rootVC isKindOfClass:[UINavigationController class]]) {
  131. rootVC = [(UINavigationController *)rootVC topViewController];
  132. }
  133. return rootVC;
  134. }
  135. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
  136. @autoreleasepool
  137. {
  138. viewController.hidesBottomBarWhenPushed = YES;
  139. [[self navigationViewController] pushViewController:viewController animated:animated];
  140. if (@available(iOS 15.0, *)) {
  141. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  142. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
  143. [self navigationViewController].navigationBar.scrollEdgeAppearance = appearance;
  144. }
  145. }
  146. }
  147. - (void)pushViewController:(UIViewController *)viewController withBackTitle:(NSString *)title
  148. {
  149. @autoreleasepool
  150. {
  151. viewController.hidesBottomBarWhenPushed = YES;
  152. [[self navigationViewController] pushViewController:viewController withBackTitle:title animated:NO];
  153. }
  154. }
  155. //- (void)pushViewController:(UIViewController *)viewController withBackTitle:(NSString *)title backAction:(FWVoidBlock)action
  156. //{
  157. // @autoreleasepool
  158. // {
  159. // viewController.hidesBottomBarWhenPushed = YES;
  160. // [[self navigationViewController] pushViewController:viewController withBackTitle:title action:action animated:NO];
  161. // }
  162. //}
  163. - (UIViewController *)popViewController
  164. {
  165. return [[self navigationViewController] popViewControllerAnimated:NO];
  166. }
  167. - (NSArray *)popToRootViewController
  168. {
  169. return [[self navigationViewController] popToRootViewControllerAnimated:NO];
  170. }
  171. - (NSArray *)popToViewController:(UIViewController *)viewController
  172. {
  173. return [[self navigationViewController] popToViewController:viewController animated:NO];
  174. }
  175. - (void)presentViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)())completion
  176. {
  177. UIViewController *top = [self topViewController];
  178. if (vc.navigationController == nil)
  179. {
  180. NavigationViewController *nav = [[NavigationViewController alloc] initWithRootViewController:vc];
  181. if (@available(iOS 15.0, *)) {
  182. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  183. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
  184. appearance.backgroundColor = kWhiteColor;
  185. nav.navigationBar.scrollEdgeAppearance = appearance;
  186. }
  187. [top presentViewController:nav animated:animated completion:completion];
  188. }
  189. else
  190. {
  191. [top presentViewController:vc animated:animated completion:completion];
  192. }
  193. }
  194. - (void)dismissViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)())completion
  195. {
  196. if (vc.navigationController != [BGBaseAppDelegate sharedAppDelegate].navigationViewController)
  197. {
  198. [vc dismissViewControllerAnimated:YES completion:nil];
  199. }
  200. else
  201. {
  202. [self popViewController];
  203. }
  204. }
  205. @end