AdBase.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // AdBase.m
  3. // XenonSDK
  4. //
  5. // Created by fq on 2020/12/23.
  6. // Copyright © 2020 SAGESSE. All rights reserved.
  7. //
  8. #import "AdBase.h"
  9. @implementation AdBase
  10. - (instancetype)init
  11. {
  12. self = [super init];
  13. if (self) {
  14. self.weight = 1;
  15. }
  16. return self;
  17. }
  18. -(BOOL)isReady {
  19. return NO;
  20. }
  21. -(UIWindow*)currentWindow{
  22. id appDelegate = [UIApplication sharedApplication].delegate;
  23. if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
  24. return [appDelegate window];
  25. }
  26. NSArray *windows = [UIApplication sharedApplication].windows;
  27. if ([windows count] == 1) {
  28. return [windows firstObject];
  29. } else {
  30. for (UIWindow *window in windows) {
  31. if (window.windowLevel == UIWindowLevelNormal) {
  32. return window;
  33. }
  34. }
  35. }
  36. return nil;
  37. }
  38. - (UIViewController *)currentViewController
  39. {
  40. UIWindow *window = [[UIApplication sharedApplication].delegate window];
  41. UIViewController *topViewController = [window rootViewController];
  42. while (true) {
  43. if (topViewController.presentedViewController) {
  44. topViewController = topViewController.presentedViewController;
  45. } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
  46. topViewController = [(UINavigationController *)topViewController topViewController];
  47. } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
  48. UITabBarController *tab = (UITabBarController *)topViewController;
  49. topViewController = tab.selectedViewController;
  50. } else {
  51. break;
  52. }
  53. }
  54. return topViewController;
  55. }
  56. @end