GDTCORLifecycle.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORLifecycle.h"
  17. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
  20. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  21. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
  22. @implementation GDTCORLifecycle
  23. + (void)load {
  24. [self sharedInstance];
  25. }
  26. /** Creates/returns the singleton instance of this class.
  27. *
  28. * @return The singleton instance of this class.
  29. */
  30. + (instancetype)sharedInstance {
  31. static GDTCORLifecycle *sharedInstance;
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. sharedInstance = [[GDTCORLifecycle alloc] init];
  35. });
  36. return sharedInstance;
  37. }
  38. - (instancetype)init {
  39. self = [super init];
  40. if (self) {
  41. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  42. [notificationCenter addObserver:self
  43. selector:@selector(applicationDidEnterBackground:)
  44. name:kGDTCORApplicationDidEnterBackgroundNotification
  45. object:nil];
  46. [notificationCenter addObserver:self
  47. selector:@selector(applicationWillEnterForeground:)
  48. name:kGDTCORApplicationWillEnterForegroundNotification
  49. object:nil];
  50. NSString *name = kGDTCORApplicationWillTerminateNotification;
  51. [notificationCenter addObserver:self
  52. selector:@selector(applicationWillTerminate:)
  53. name:name
  54. object:nil];
  55. }
  56. return self;
  57. }
  58. - (void)dealloc {
  59. [[NSNotificationCenter defaultCenter] removeObserver:self];
  60. }
  61. - (void)applicationDidEnterBackground:(NSNotification *)notification {
  62. GDTCORApplication *application = [GDTCORApplication sharedApplication];
  63. if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  64. GDTCORLogDebug(@"%@", @"Signaling GDTCORTransformer that the app is backgrounding.");
  65. [[GDTCORTransformer sharedInstance] appWillBackground:application];
  66. }
  67. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  68. GDTCORLogDebug(@"%@", @"Signaling GDTCORUploadCoordinator that the app is backgrounding.");
  69. [[GDTCORUploadCoordinator sharedInstance] appWillBackground:application];
  70. }
  71. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  72. GDTCORLogDebug(@"%@", @"Signaling GDTCORRegistrar that the app is backgrounding.");
  73. [[GDTCORRegistrar sharedInstance] appWillBackground:application];
  74. }
  75. }
  76. - (void)applicationWillEnterForeground:(NSNotification *)notification {
  77. GDTCORApplication *application = [GDTCORApplication sharedApplication];
  78. if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  79. GDTCORLogDebug(@"%@", @"Signaling GDTCORTransformer that the app is foregrounding.");
  80. [[GDTCORTransformer sharedInstance] appWillForeground:application];
  81. }
  82. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  83. GDTCORLogDebug(@"%@", @"Signaling GDTCORUploadCoordinator that the app is foregrounding.");
  84. [[GDTCORUploadCoordinator sharedInstance] appWillForeground:application];
  85. }
  86. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  87. GDTCORLogDebug(@"%@", @"Signaling GDTCORRegistrar that the app is foregrounding.");
  88. [[GDTCORRegistrar sharedInstance] appWillForeground:application];
  89. }
  90. }
  91. - (void)applicationWillTerminate:(NSNotification *)notification {
  92. GDTCORApplication *application = [GDTCORApplication sharedApplication];
  93. if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  94. GDTCORLogDebug(@"%@", @"Signaling GDTCORTransformer that the app is terminating.");
  95. [[GDTCORTransformer sharedInstance] appWillTerminate:application];
  96. }
  97. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  98. GDTCORLogDebug(@"%@", @"Signaling GDTCORUploadCoordinator that the app is terminating.");
  99. [[GDTCORUploadCoordinator sharedInstance] appWillTerminate:application];
  100. }
  101. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  102. GDTCORLogDebug(@"%@", @"Signaling GDTCORRegistrar that the app is terminating.");
  103. [[GDTCORRegistrar sharedInstance] appWillTerminate:application];
  104. }
  105. }
  106. @end