Files
react-native/React/CoreModules/RCTStatusBarManager.mm
T
radex 80e6d672f3 UIViewController-based status bar management (#25919)
Summary:
{emoji:26a0} This is a follow up to https://github.com/facebook/react-native/issues/25425 -- which isn't merged yet… See https://github.com/facebook/react-native/pull/25919/files/2a286257a6553a80a34e2b1f1ad94fc7bae36ea3..125aedbedc234c65c8d1b2133b79e926ad6cf145 for actual diff

Currently, StatusBar native module manages the status bar on iOS globally, using `UIApplication.` APIs. This is bad because:

- those APIs have been deprecated for 4 years
- Apple really, really wants you to have an explicitly defined view controller, and control the status bar there
- it [breaks external native components](https://github.com/facebook/react-native/issues/25181#issuecomment-506792819)
- it's [not compatible with iPadOS 13 multi window support](https://github.com/facebook/react-native/issues/25181#issuecomment-506690818)

for those reasons I we should transition towards view controller-based status bar management.

With that, there is a need to introduce a default React Native root view controller, so I added `RCTRootViewController`. Using it is completely opt-in and there is no breaking change here. However I believe this should be a part of the template for new RN iOS apps.

Additionally, I added `RCTRootViewControllerProtocol` with hooks needed for RCTStatusBarManager to control the status bar. This means apps that want to have total control over their view controller can still opt in to react native VC-based status bar by conforming their root view controller to this protocol.

## Changelog

[iOS] [Added] - Added `RCTRootViewController` and `RCTRootViewControllerProtocol`
[iOS] [Fixed] - `UIViewControllerBasedStatusBarAppearance=YES` no longer triggers an error as long as you use `RCTRootViewController`
[iOS] [Fixed] - Status bar style is now correctly changed in multi-window iPadOS 13 apps if you use `RCTRootViewController` and set `UIViewControllerBasedStatusBarAppearance=YES`
Pull Request resolved: https://github.com/facebook/react-native/pull/25919

Test Plan: - Open RNTester → StatusBar → and check that no features broke

Reviewed By: fkgozali

Differential Revision: D16957766

Pulled By: hramos

fbshipit-source-id: 9ae1384ee20a06933053c4404b8237810f1e7c2c
2020-03-04 14:25:12 -08:00

251 lines
7.7 KiB
Plaintext

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTStatusBarManager.h"
#import "CoreModulesPlugins.h"
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
#import <React/RCTUIManager.h>
#import <React/RCTUtils.h>
#import <React/RCTRootViewController.h>
#if !TARGET_OS_TV
#import <FBReactNativeSpec/FBReactNativeSpec.h>
@implementation RCTConvert (UIStatusBar)
+ (UIStatusBarStyle)UIStatusBarStyle:(id)json RCT_DYNAMIC
{
static NSDictionary *mapping;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(iOS 13.0, *)) {
mapping = @{
@"default" : @(UIStatusBarStyleDefault),
@"light-content" : @(UIStatusBarStyleLightContent),
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
@"dark-content" : @(UIStatusBarStyleDarkContent)
#else
@"dark-content": @(UIStatusBarStyleDefault)
#endif
};
} else {
mapping = @{
@"default" : @(UIStatusBarStyleDefault),
@"light-content" : @(UIStatusBarStyleLightContent),
@"dark-content" : @(UIStatusBarStyleDefault)
};
}
});
return _RCT_CAST(
UIStatusBarStyle, [RCTConvertEnumValue("UIStatusBarStyle", mapping, @(UIStatusBarStyleDefault), json) integerValue]);
}
RCT_ENUM_CONVERTER(
UIStatusBarAnimation,
(@{
@"none" : @(UIStatusBarAnimationNone),
@"fade" : @(UIStatusBarAnimationFade),
@"slide" : @(UIStatusBarAnimationSlide),
}),
UIStatusBarAnimationNone,
integerValue);
@end
#endif
#if !TARGET_OS_TV
@interface RCTStatusBarManager() <NativeStatusBarManagerIOSSpec>
@end
#endif
@implementation RCTStatusBarManager
static BOOL RCTViewControllerBasedStatusBarAppearance()
{
static BOOL value;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
value =
[[[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"]
?: @YES boolValue];
});
return value;
}
RCT_EXPORT_MODULE()
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
- (NSArray<NSString *> *)supportedEvents
{
return @[ @"statusBarFrameDidChange", @"statusBarFrameWillChange" ];
}
#if !TARGET_OS_TV
- (void)startObserving
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(applicationDidChangeStatusBarFrame:)
name:UIApplicationDidChangeStatusBarFrameNotification
object:nil];
[nc addObserver:self
selector:@selector(applicationWillChangeStatusBarFrame:)
name:UIApplicationWillChangeStatusBarFrameNotification
object:nil];
}
- (void)stopObserving
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (void)emitEvent:(NSString *)eventName forNotification:(NSNotification *)notification
{
CGRect frame = [notification.userInfo[UIApplicationStatusBarFrameUserInfoKey] CGRectValue];
NSDictionary *event = @{
@"frame" : @{
@"x" : @(frame.origin.x),
@"y" : @(frame.origin.y),
@"width" : @(frame.size.width),
@"height" : @(frame.size.height),
},
};
[self sendEventWithName:eventName body:event];
}
- (void)applicationDidChangeStatusBarFrame:(NSNotification *)notification
{
[self emitEvent:@"statusBarFrameDidChange" forNotification:notification];
}
- (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification
{
[self emitEvent:@"statusBarFrameWillChange" forNotification:notification];
}
- (UIViewController<RCTRootViewControllerProtocol>*) viewControllerForReactTag:(nonnull NSNumber *)reactTag
{
if (!RCTViewControllerBasedStatusBarAppearance()) {
return nil;
}
UIView *view = [self.bridge.uiManager viewForReactTag:reactTag];
UIViewController *viewController = view.window.rootViewController ?: RCTKeyWindow().rootViewController;
if ([viewController conformsToProtocol:@protocol(RCTRootViewControllerProtocol)]) {
return (UIViewController<RCTRootViewControllerProtocol>*) viewController;
} else {
RCTLogError(@"RCTStatusBarManager could not find RCTRootViewController. \
If UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to YES (recommended for new apps), \
You need to use RCTRootViewControllerProtocol-conforming view controller as app window's root view controller \
and must pass a node reference to `surface` argument of StatusBar methods.");
return nil;
}
}
RCT_EXPORT_METHOD(getHeight:(RCTResponseSenderBlock)callback)
{
callback(@[ @{
@"height" : @(RCTSharedApplication().statusBarFrame.size.height),
} ]);
}
RCT_EXPORT_METHOD(setStyle:(NSString *)style
animated:(BOOL)animated
reactTag:(double)reactTag)
{
// NSNumber *reactTag = options.reactTag() ? @(options.reactTag()) : @-1;
UIStatusBarStyle statusBarStyle = [RCTConvert UIStatusBarStyle:style];
UIViewController<RCTRootViewControllerProtocol> *viewController = [self viewControllerForReactTag:@(reactTag)];
if (viewController) {
[viewController updateStatusBarStyle:statusBarStyle
hidden:viewController.prefersStatusBarHidden
animation:viewController.preferredStatusBarUpdateAnimation
animated:animated];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[RCTSharedApplication() setStatusBarStyle:statusBarStyle
animated:animated];
#pragma clang diagnostic pop
}
}
RCT_EXPORT_METHOD(setHidden:(BOOL)hidden
withAnimation:(NSString *)withAnimation
reactTag:(double)reactTag)
{
UIStatusBarAnimation animation = [RCTConvert UIStatusBarAnimation:withAnimation];
UIViewController<RCTRootViewControllerProtocol> *viewController = [self viewControllerForReactTag:@(reactTag)];
if (viewController) {
[viewController updateStatusBarStyle:viewController.preferredStatusBarStyle
hidden:hidden
animation:animation
animated:animation != UIStatusBarAnimationNone];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[RCTSharedApplication() setStatusBarHidden:hidden
withAnimation:animation];
#pragma clang diagnostic pop
}
}
RCT_EXPORT_METHOD(setNetworkActivityIndicatorVisible : (BOOL)visible)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
RCTSharedApplication().networkActivityIndicatorVisible = visible;
#pragma clang diagnostic pop
}
- (facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants>)getConstants
{
return facebook::react::typedConstants<JS::NativeStatusBarManagerIOS::Constants>({
.HEIGHT = RCTSharedApplication().statusBarFrame.size.height,
.DEFAULT_BACKGROUND_COLOR = folly::none,
});
}
- (facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants>)constantsToExport
{
return (facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants>)[self getConstants];
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
{
return std::make_shared<facebook::react::NativeStatusBarManagerIOSSpecJSI>(self, jsInvoker);
}
#endif // TARGET_OS_TV
@end
Class RCTStatusBarManagerCls(void) {
return RCTStatusBarManager.class;
}