GDTCORPlatform.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. @implementation GDTCORApplication
  33. + (void)load {
  34. #if TARGET_OS_IOS || TARGET_OS_TV
  35. // If this asserts, please file a bug at https://github.com/firebase/firebase-ios-sdk/issues.
  36. GDTCORFatalAssert(
  37. GDTCORBackgroundIdentifierInvalid == UIBackgroundTaskInvalid,
  38. @"GDTCORBackgroundIdentifierInvalid and UIBackgroundTaskInvalid should be the same.");
  39. #endif
  40. [self sharedApplication];
  41. }
  42. + (nullable GDTCORApplication *)sharedApplication {
  43. static GDTCORApplication *application;
  44. static dispatch_once_t onceToken;
  45. dispatch_once(&onceToken, ^{
  46. application = [[GDTCORApplication alloc] init];
  47. });
  48. return application;
  49. }
  50. - (instancetype)init {
  51. self = [super init];
  52. if (self) {
  53. #if TARGET_OS_IOS || TARGET_OS_TV
  54. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  55. [notificationCenter addObserver:self
  56. selector:@selector(iOSApplicationDidEnterBackground:)
  57. name:UIApplicationDidEnterBackgroundNotification
  58. object:nil];
  59. [notificationCenter addObserver:self
  60. selector:@selector(iOSApplicationWillEnterForeground:)
  61. name:UIApplicationWillEnterForegroundNotification
  62. object:nil];
  63. NSString *name = UIApplicationWillTerminateNotification;
  64. [notificationCenter addObserver:self
  65. selector:@selector(iOSApplicationWillTerminate:)
  66. name:name
  67. object:nil];
  68. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  69. if (@available(iOS 13, tvOS 13.0, *)) {
  70. [notificationCenter addObserver:self
  71. selector:@selector(iOSApplicationWillEnterForeground:)
  72. name:UISceneWillEnterForegroundNotification
  73. object:nil];
  74. [notificationCenter addObserver:self
  75. selector:@selector(iOSApplicationDidEnterBackground:)
  76. name:UISceneWillDeactivateNotification
  77. object:nil];
  78. }
  79. #endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  80. #elif TARGET_OS_OSX
  81. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  82. [notificationCenter addObserver:self
  83. selector:@selector(macOSApplicationWillTerminate:)
  84. name:NSApplicationWillTerminateNotification
  85. object:nil];
  86. #endif // TARGET_OS_IOS || TARGET_OS_TV
  87. }
  88. return self;
  89. }
  90. - (GDTCORBackgroundIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler {
  91. return
  92. [[self sharedApplicationForBackgroundTask] beginBackgroundTaskWithExpirationHandler:handler];
  93. }
  94. - (void)endBackgroundTask:(GDTCORBackgroundIdentifier)bgID {
  95. if (bgID != GDTCORBackgroundIdentifierInvalid) {
  96. [[self sharedApplicationForBackgroundTask] endBackgroundTask:bgID];
  97. }
  98. }
  99. #pragma mark - App environment helpers
  100. - (BOOL)isAppExtension {
  101. #if TARGET_OS_IOS || TARGET_OS_TV
  102. BOOL appExtension = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"];
  103. return appExtension;
  104. #elif TARGET_OS_OSX
  105. return NO;
  106. #endif
  107. }
  108. /** Returns a UIApplication instance if on the appropriate platform.
  109. *
  110. * @return The shared UIApplication if on the appropriate platform.
  111. */
  112. #if TARGET_OS_IOS || TARGET_OS_TV
  113. - (nullable UIApplication *)sharedApplicationForBackgroundTask {
  114. #else
  115. - (nullable id)sharedApplicationForBackgroundTask {
  116. #endif
  117. if ([self isAppExtension]) {
  118. return nil;
  119. }
  120. id sharedApplication = nil;
  121. Class uiApplicationClass = NSClassFromString(@"UIApplication");
  122. if (uiApplicationClass &&
  123. [uiApplicationClass respondsToSelector:(NSSelectorFromString(@"sharedApplication"))]) {
  124. sharedApplication = [uiApplicationClass sharedApplication];
  125. }
  126. return sharedApplication;
  127. }
  128. #pragma mark - UIApplicationDelegate
  129. #if TARGET_OS_IOS || TARGET_OS_TV
  130. - (void)iOSApplicationDidEnterBackground:(NSNotification *)notif {
  131. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  132. [notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
  133. }
  134. - (void)iOSApplicationWillEnterForeground:(NSNotification *)notif {
  135. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  136. [notifCenter postNotificationName:kGDTCORApplicationWillEnterForegroundNotification object:nil];
  137. }
  138. - (void)iOSApplicationWillTerminate:(NSNotification *)notif {
  139. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  140. [notifCenter postNotificationName:kGDTCORApplicationWillTerminateNotification object:nil];
  141. }
  142. #endif // TARGET_OS_IOS || TARGET_OS_TV
  143. #pragma mark - NSApplicationDelegate
  144. #if TARGET_OS_OSX
  145. - (void)macOSApplicationWillTerminate:(NSNotification *)notif {
  146. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  147. [notifCenter postNotificationName:kGDTCORApplicationWillTerminateNotification object:nil];
  148. }
  149. #endif // TARGET_OS_OSX
  150. @end