mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
2f9bd1f62f
Summary: This adds workarounds for the code that was preventing React from compiling when linked against an iOS App Extension target. Some iOS APIs are unavailable to App Extensions, and Xcode's static analysis will catch attempts to use methods that have been flagged as unavailable. React currently uses two APIs that are off limits to extensions: `[UIApplication sharedApplication]` and `[UIAlertView initWith ...]`. This commit adds a helper function to `RCTUtils.[hm]` called `RCTRunningInAppExtension()`, which returns `YES` if, at runtime, it can be determined that we're running in an app extension (by checking whether the path to `[NSBundle mainBundle]` has the `"appex"` path extension). It also adds a `RCTSharedApplication()` function, which will return `nil` if running in an App Extension. If running in an App, `RCTSharedApplication()` calls `sharedApplication` by calling `performSelector:` on the `UIApplication` class. This passes the static analysis check, and, in my opinion, obeys the "spirit of th Closes https://github.com/facebook/react-native/pull/1895 Reviewed By: @svcscm Differential Revision: D2224128 Pulled By: @nicklockwood
114 lines
3.0 KiB
Objective-C
114 lines
3.0 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import "RCTAppState.h"
|
|
|
|
#import "RCTAssert.h"
|
|
#import "RCTBridge.h"
|
|
#import "RCTEventDispatcher.h"
|
|
#import "RCTUtils.h"
|
|
|
|
static NSString *RCTCurrentAppBackgroundState()
|
|
{
|
|
static NSDictionary *states;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
states = @{
|
|
@(UIApplicationStateActive): @"active",
|
|
@(UIApplicationStateBackground): @"background",
|
|
@(UIApplicationStateInactive): @"inactive"
|
|
};
|
|
});
|
|
|
|
if (RCTRunningInAppExtension()) {
|
|
return @"extension";
|
|
}
|
|
|
|
return states[@(RCTSharedApplication().applicationState)] ?: @"unknown";
|
|
}
|
|
|
|
@implementation RCTAppState
|
|
{
|
|
NSString *_lastKnownState;
|
|
}
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
#pragma mark - Lifecycle
|
|
|
|
- (instancetype)init
|
|
{
|
|
if ((self = [super init])) {
|
|
|
|
_lastKnownState = RCTCurrentAppBackgroundState();
|
|
|
|
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
|
|
UIApplicationDidEnterBackgroundNotification,
|
|
UIApplicationDidFinishLaunchingNotification]) {
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(handleAppStateDidChange)
|
|
name:name
|
|
object:nil];
|
|
}
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(handleMemoryWarning)
|
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
|
object:nil];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)handleMemoryWarning
|
|
{
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"memoryWarning"
|
|
body:nil];
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
}
|
|
|
|
#pragma mark - App Notification Methods
|
|
|
|
- (void)handleAppStateDidChange
|
|
{
|
|
NSString *newState = RCTCurrentAppBackgroundState();
|
|
if (![newState isEqualToString:_lastKnownState]) {
|
|
_lastKnownState = newState;
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"appStateDidChange"
|
|
body:@{@"app_state": _lastKnownState}];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Public API
|
|
|
|
/**
|
|
* Get the current background/foreground state of the app
|
|
*/
|
|
RCT_EXPORT_METHOD(getCurrentAppState:(RCTResponseSenderBlock)callback
|
|
error:(__unused RCTResponseSenderBlock)error)
|
|
{
|
|
callback(@[@{@"app_state": _lastKnownState}]);
|
|
}
|
|
|
|
#pragma mark - RCTBridgeModule
|
|
|
|
- (NSDictionary *)constantsToExport
|
|
{
|
|
return @{
|
|
@"initialAppState" : RCTCurrentAppBackgroundState()
|
|
};
|
|
}
|
|
|
|
@end
|