GDTCORPlatform.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/GDTCORPlatform.h>
  17. #import <GoogleDataTransport/GDTCORAssert.h>
  18. const GDTCORBackgroundIdentifier GDTCORBackgroundIdentifierInvalid = 0;
  19. NSString *const kGDTCORApplicationDidEnterBackgroundNotification =
  20. @"GDTCORApplicationDidEnterBackgroundNotification";
  21. NSString *const kGDTCORApplicationWillEnterForegroundNotification =
  22. @"GDTCORApplicationWillEnterForegroundNotification";
  23. NSString *const kGDTCORApplicationWillTerminateNotification =
  24. @"GDTCORApplicationWillTerminateNotification";
  25. BOOL GDTCORReachabilityFlagsContainWWAN(SCNetworkReachabilityFlags flags) {
  26. #if TARGET_OS_IOS
  27. return (flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN;
  28. #else
  29. return NO;
  30. #endif // TARGET_OS_IOS
  31. }
  32. @interface GDTCORApplication ()
  33. /**
  34. Private flag to match the existing `readonly` public flag. This will be accurate for all platforms,
  35. since we handle each platform's lifecycle notifications separately.
  36. */
  37. @property(atomic, readwrite) BOOL isRunningInBackground;
  38. @end
  39. @implementation GDTCORApplication
  40. + (void)load {
  41. #if TARGET_OS_IOS || TARGET_OS_TV
  42. // If this asserts, please file a bug at https://github.com/firebase/firebase-ios-sdk/issues.
  43. GDTCORFatalAssert(
  44. GDTCORBackgroundIdentifierInvalid == UIBackgroundTaskInvalid,
  45. @"GDTCORBackgroundIdentifierInvalid and UIBackgroundTaskInvalid should be the same.");
  46. #endif
  47. [self sharedApplication];
  48. }
  49. + (nullable GDTCORApplication *)sharedApplication {
  50. static GDTCORApplication *application;
  51. static dispatch_once_t onceToken;
  52. dispatch_once(&onceToken, ^{
  53. application = [[GDTCORApplication alloc] init];
  54. });
  55. return application;
  56. }
  57. - (instancetype)init {
  58. self = [super init];
  59. if (self) {
  60. // This class will be instantiated in the foreground.
  61. _isRunningInBackground = NO;
  62. #if TARGET_OS_IOS || TARGET_OS_TV
  63. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  64. [notificationCenter addObserver:self
  65. selector:@selector(iOSApplicationDidEnterBackground:)
  66. name:UIApplicationDidEnterBackgroundNotification
  67. object:nil];
  68. [notificationCenter addObserver:self
  69. selector:@selector(iOSApplicationWillEnterForeground:)
  70. name:UIApplicationWillEnterForegroundNotification
  71. object:nil];
  72. NSString *name = UIApplicationWillTerminateNotification;
  73. [notificationCenter addObserver:self
  74. selector:@selector(iOSApplicationWillTerminate:)
  75. name:name
  76. object:nil];
  77. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  78. if (@available(iOS 13, tvOS 13.0, *)) {
  79. [notificationCenter addObserver:self
  80. selector:@selector(iOSApplicationWillEnterForeground:)
  81. name:UISceneWillEnterForegroundNotification
  82. object:nil];
  83. [notificationCenter addObserver:self
  84. selector:@selector(iOSApplicationDidEnterBackground:)
  85. name:UISceneWillDeactivateNotification
  86. object:nil];
  87. }
  88. #endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  89. #elif TARGET_OS_OSX
  90. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  91. [notificationCenter addObserver:self
  92. selector:@selector(macOSApplicationWillTerminate:)
  93. name:NSApplicationWillTerminateNotification
  94. object:nil];
  95. #endif // TARGET_OS_IOS || TARGET_OS_TV
  96. }
  97. return self;
  98. }
  99. - (GDTCORBackgroundIdentifier)beginBackgroundTaskWithName:(NSString *)name
  100. expirationHandler:(void (^)(void))handler {
  101. return [[self sharedApplicationForBackgroundTask] beginBackgroundTaskWithName:name
  102. expirationHandler:handler];
  103. }
  104. - (void)endBackgroundTask:(GDTCORBackgroundIdentifier)bgID {
  105. if (bgID != GDTCORBackgroundIdentifierInvalid) {
  106. [[self sharedApplicationForBackgroundTask] endBackgroundTask:bgID];
  107. }
  108. }
  109. #pragma mark - App environment helpers
  110. - (BOOL)isAppExtension {
  111. #if TARGET_OS_IOS || TARGET_OS_TV
  112. BOOL appExtension = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"];
  113. return appExtension;
  114. #elif TARGET_OS_OSX
  115. return NO;
  116. #endif
  117. }
  118. /** Returns a UIApplication instance if on the appropriate platform.
  119. *
  120. * @return The shared UIApplication if on the appropriate platform.
  121. */
  122. #if TARGET_OS_IOS || TARGET_OS_TV
  123. - (nullable UIApplication *)sharedApplicationForBackgroundTask {
  124. #else
  125. - (nullable id)sharedApplicationForBackgroundTask {
  126. #endif
  127. if ([self isAppExtension]) {
  128. return nil;
  129. }
  130. id sharedApplication = nil;
  131. Class uiApplicationClass = NSClassFromString(@"UIApplication");
  132. if (uiApplicationClass &&
  133. [uiApplicationClass respondsToSelector:(NSSelectorFromString(@"sharedApplication"))]) {
  134. sharedApplication = [uiApplicationClass sharedApplication];
  135. }
  136. return sharedApplication;
  137. }
  138. #pragma mark - UIApplicationDelegate
  139. #if TARGET_OS_IOS || TARGET_OS_TV
  140. - (void)iOSApplicationDidEnterBackground:(NSNotification *)notif {
  141. _isRunningInBackground = YES;
  142. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  143. [notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
  144. }
  145. - (void)iOSApplicationWillEnterForeground:(NSNotification *)notif {
  146. _isRunningInBackground = NO;
  147. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  148. [notifCenter postNotificationName:kGDTCORApplicationWillEnterForegroundNotification object:nil];
  149. }
  150. - (void)iOSApplicationWillTerminate:(NSNotification *)notif {
  151. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  152. [notifCenter postNotificationName:kGDTCORApplicationWillTerminateNotification object:nil];
  153. }
  154. #endif // TARGET_OS_IOS || TARGET_OS_TV
  155. #pragma mark - NSApplicationDelegate
  156. #if TARGET_OS_OSX
  157. - (void)macOSApplicationWillTerminate:(NSNotification *)notif {
  158. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  159. [notifCenter postNotificationName:kGDTCORApplicationWillTerminateNotification object:nil];
  160. }
  161. #endif // TARGET_OS_OSX
  162. @end