RCTAppState.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTAppState.h"
  8. #import "RCTAssert.h"
  9. #import "RCTBridge.h"
  10. #import "RCTEventDispatcher.h"
  11. #import "RCTUtils.h"
  12. static NSString *RCTCurrentAppBackgroundState()
  13. {
  14. static NSDictionary *states;
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. states = @{
  18. @(UIApplicationStateActive): @"active",
  19. @(UIApplicationStateBackground): @"background"
  20. };
  21. });
  22. if (RCTRunningInAppExtension()) {
  23. return @"extension";
  24. }
  25. return states[@(RCTSharedApplication().applicationState)] ?: @"unknown";
  26. }
  27. @implementation RCTAppState
  28. {
  29. NSString *_lastKnownState;
  30. }
  31. RCT_EXPORT_MODULE()
  32. + (BOOL)requiresMainQueueSetup
  33. {
  34. return YES;
  35. }
  36. - (dispatch_queue_t)methodQueue
  37. {
  38. return dispatch_get_main_queue();
  39. }
  40. - (NSDictionary *)constantsToExport
  41. {
  42. return [self getConstants];
  43. }
  44. - (NSDictionary *)getConstants
  45. {
  46. return @{@"initialAppState": RCTCurrentAppBackgroundState()};
  47. }
  48. #pragma mark - Lifecycle
  49. - (NSArray<NSString *> *)supportedEvents
  50. {
  51. return @[@"appStateDidChange", @"memoryWarning"];
  52. }
  53. - (void)startObserving
  54. {
  55. for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
  56. UIApplicationDidEnterBackgroundNotification,
  57. UIApplicationDidFinishLaunchingNotification,
  58. UIApplicationWillResignActiveNotification,
  59. UIApplicationWillEnterForegroundNotification]) {
  60. [[NSNotificationCenter defaultCenter] addObserver:self
  61. selector:@selector(handleAppStateDidChange:)
  62. name:name
  63. object:nil];
  64. }
  65. [[NSNotificationCenter defaultCenter] addObserver:self
  66. selector:@selector(handleMemoryWarning)
  67. name:UIApplicationDidReceiveMemoryWarningNotification
  68. object:nil];
  69. }
  70. - (void)stopObserving
  71. {
  72. [[NSNotificationCenter defaultCenter] removeObserver:self];
  73. }
  74. #pragma mark - App Notification Methods
  75. - (void)handleMemoryWarning
  76. {
  77. [self sendEventWithName:@"memoryWarning" body:nil];
  78. }
  79. - (void)handleAppStateDidChange:(NSNotification *)notification
  80. {
  81. NSString *newState;
  82. if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
  83. newState = @"inactive";
  84. } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
  85. newState = @"background";
  86. } else {
  87. newState = RCTCurrentAppBackgroundState();
  88. }
  89. if (![newState isEqualToString:_lastKnownState]) {
  90. _lastKnownState = newState;
  91. [self sendEventWithName:@"appStateDidChange"
  92. body:@{@"app_state": _lastKnownState}];
  93. }
  94. }
  95. #pragma mark - Public API
  96. /**
  97. * Get the current background/foreground state of the app
  98. */
  99. RCT_EXPORT_METHOD(getCurrentAppState:(RCTResponseSenderBlock)callback
  100. error:(__unused RCTResponseSenderBlock)error)
  101. {
  102. callback(@[@{@"app_state": RCTCurrentAppBackgroundState()}]);
  103. }
  104. @end