From ec9efb8a01ce59c7a594df5ca4c314a208ba1902 Mon Sep 17 00:00:00 2001 From: Ken Wheeler Date: Thu, 10 Mar 2016 08:29:52 -0800 Subject: [PATCH] Updating AppState to support the inactive state. Summary:**Motivation** AppStateIOS never currently returns `inactive` as a possible state. I had a requirement that when inactive, certain portions of the app should be blacked out in accordance with compliance rules. This is not possible currently, due to `inactive` never being returned. This PR fixes that. **Test plan** All base tests are passing. Are there AppState specific tests in place at the moment that I'm missing? **Demonstration** ![appstate](https://cloud.githubusercontent.com/assets/286616/13640546/1cb6eeb0-e5e3-11e5-8d64-332ea3383a54.gif) Closes https://github.com/facebook/react-native/pull/6379 Differential Revision: D3035530 Pulled By: nicklockwood fb-gh-sync-id: 93deccc8184816809926dca8a95f2bebd1434987 shipit-source-id: 93deccc8184816809926dca8a95f2bebd1434987 --- Libraries/AppStateIOS/AppStateIOS.ios.js | 5 +++-- React/Modules/RCTAppState.m | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Libraries/AppStateIOS/AppStateIOS.ios.js b/Libraries/AppStateIOS/AppStateIOS.ios.js index 82d968ce9..807f7701e 100644 --- a/Libraries/AppStateIOS/AppStateIOS.ios.js +++ b/Libraries/AppStateIOS/AppStateIOS.ios.js @@ -35,8 +35,9 @@ var _eventHandlers = { * - `active` - The app is running in the foreground * - `background` - The app is running in the background. The user is either * in another app or on the home screen - * - `inactive` - This is a transition state that currently never happens for - * typical React Native apps. + * - `inactive` - This is a state that occurs when transitioning between + * foreground & background, and during periods of inactivity such as + * entering the Multitasking view or in the event of an incoming call * * For more information, see * [Apple's documentation](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html) diff --git a/React/Modules/RCTAppState.m b/React/Modules/RCTAppState.m index 6a22ae626..a71e2b3a8 100644 --- a/React/Modules/RCTAppState.m +++ b/React/Modules/RCTAppState.m @@ -21,8 +21,7 @@ static NSString *RCTCurrentAppBackgroundState() dispatch_once(&onceToken, ^{ states = @{ @(UIApplicationStateActive): @"active", - @(UIApplicationStateBackground): @"background", - @(UIApplicationStateInactive): @"inactive" + @(UIApplicationStateBackground): @"background" }; }); @@ -53,9 +52,12 @@ RCT_EXPORT_MODULE() for (NSString *name in @[UIApplicationDidBecomeActiveNotification, UIApplicationDidEnterBackgroundNotification, - UIApplicationDidFinishLaunchingNotification]) { + UIApplicationDidFinishLaunchingNotification, + UIApplicationWillResignActiveNotification, + UIApplicationWillEnterForegroundNotification]) { + [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleAppStateDidChange) + selector:@selector(handleAppStateDidChange:) name:name object:nil]; } @@ -79,9 +81,18 @@ RCT_EXPORT_MODULE() #pragma mark - App Notification Methods -- (void)handleAppStateDidChange +- (void)handleAppStateDidChange:(NSNotification *)notification { - NSString *newState = RCTCurrentAppBackgroundState(); + NSString *newState; + + if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) { + newState = @"inactive"; + } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) { + newState = @"active"; + } else { + newState = RCTCurrentAppBackgroundState(); + } + if (![newState isEqualToString:_lastKnownState]) { _lastKnownState = newState; [_bridge.eventDispatcher sendDeviceEventWithName:@"appStateDidChange"