GDTCORLifecycle.m 4.7 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 "GDTCORLibrary/Public/GDTCORLifecycle.h"
  17. #import <GoogleDataTransport/GDTCOREvent.h>
  18. #import "GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
  19. #import "GDTCORLibrary/Private/GDTCORStorage_Private.h"
  20. #import "GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  21. #import "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. [[GDTCORTransformer sharedInstance] appWillBackground:application];
  65. }
  66. if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  67. [[GDTCORStorage sharedInstance] appWillBackground:application];
  68. }
  69. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  70. [[GDTCORUploadCoordinator sharedInstance] appWillBackground:application];
  71. }
  72. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  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. [[GDTCORTransformer sharedInstance] appWillForeground:application];
  80. }
  81. if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  82. [[GDTCORStorage sharedInstance] appWillForeground:application];
  83. }
  84. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  85. [[GDTCORUploadCoordinator sharedInstance] appWillForeground:application];
  86. }
  87. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  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. [[GDTCORTransformer sharedInstance] appWillTerminate:application];
  95. }
  96. if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  97. [[GDTCORStorage sharedInstance] appWillTerminate:application];
  98. }
  99. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  100. [[GDTCORUploadCoordinator sharedInstance] appWillTerminate:application];
  101. }
  102. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  103. [[GDTCORRegistrar sharedInstance] appWillTerminate:application];
  104. }
  105. }
  106. @end