Skip to content

Commit f319ff3

Browse files
devon94facebook-github-bot
authored andcommitted
iOS: Update RCTAlertManager to use new RCTAlertController (#29295)
Summary: This should fix #29082 and #10471 Currently when an alert is being shown while a modal is being dismissed, it causes the alert not to show and In some cases it causes the UI to become unresponsive. I think this was caused by using RCTPresentedViewController to try and display the Alert the currently presented view. The View the Alert was going to be shown on is dismissed and the modal doesn't show. I implemented a new RCTAlertController to show the alert on top of the view, the modal being dismissed should now not interfere with the alert being shown. ## Changelog [iOS] [Fixed] - Fixed showing Alert while closing a Modal Pull Request resolved: #29295 Test Plan: To recreate the bug: 1. npx react-native init Test --version 0.63.0-rc.1 2. Paste the following code into App.js ```javascript /** * Sample React Native App * https://github.com/facebook/react-native * * format * flow strict-local */ import React from 'react'; import { SafeAreaView, StyleSheet, View, Text, StatusBar, Modal, Alert } from 'react-native'; const App: () => React$Node = () => { const [visible, setVisible] = React.useState(false) const onShowModal = () => { setVisible(true) } onCloseBroken = () => { setVisible(false) Alert.alert('Alert', 'Alert won\'t show') } onCloseWorking = () => { setVisible(false) setTimeout(() => Alert.alert('Alert', 'Works fine'), 10) } return ( <> <StatusBar barStyle="dark-content" /> <SafeAreaView style={styles.container}> <Text onPress={onShowModal}>Show modal</Text> </SafeAreaView> <Modal animationType="fade" visible={visible} onRequestClose={onCloseWorking} > <View style={styles.container}> <Text onPress={onCloseBroken}>Close modal immediately</Text> <Text onPress={onCloseWorking}>Close modal with delay</Text> </View> </Modal> </> ) } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'space-around', }, }) export default App ``` 3. cd Test && npx react-native run-ios 4. Show the modal and click the `Close modal immediately` button The first button doesn't show the alert, the second does because it gets rendered after the modal view is dismissed. After this commit, the alert always shows on top of every view properly. You can test by pointing the react native package to my branch by modifying the package json file like this ``` "react-native": "https://github.com/devon94/react-native.git#fix-ios-modal" ``` I was unable to reproduce the case where it causes the UI to be responsive in the test app but was able to reproduce it in our react native app at work. I can provide a video later if needed but the code is too complex to simplify into a test case. Reviewed By: sammy-SC Differential Revision: D22783371 Pulled By: PeteTheHeat fbshipit-source-id: 3e359645c610074ea855ee5686c59bdb9d6b696b
1 parent 07640dc commit f319ff3

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

RNTester/Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ SPEC CHECKSUMS:
505505
RCTTypeSafety: 1ade47a69b092cddf1e4ea21e0c5bdc65cc950b4
506506
React: cafb3c2321f7df55ce90dbf29d513799a79e4418
507507
React-callinvoker: 0dada022d38b73e6e15b33e2a96476153f79bbf6
508-
React-Core: 22edbf19e3f430ce23111ead6741579f7a591ff2
508+
React-Core: 6c6f6c40bb1e031de8a0fafce08c010edfef09ab
509509
React-CoreModules: d13d148c851af5780f864be74bc2165140923dc7
510510
React-cxxreact: 4661b3295e62c6eaada084e2f826c70c71ef11ea
511511
React-jsi: fe94132da767bfc4801968c2a12abae43e9a833e
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
8+
#import <UIKit/UIKit.h>
9+
10+
@interface RCTAlertController : UIAlertController
11+
12+
- (void)show:(BOOL)animated completion:(void (^)(void))completion;
13+
14+
@end
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
8+
#import <React/RCTUtils.h>
9+
10+
#import "RCTAlertController.h"
11+
12+
@interface RCTAlertController ()
13+
14+
@property (nonatomic, strong) UIWindow *alertWindow;
15+
16+
@end
17+
18+
@implementation RCTAlertController
19+
20+
- (UIWindow *)alertWindow
21+
{
22+
if (_alertWindow == nil) {
23+
_alertWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds];
24+
_alertWindow.rootViewController = [UIViewController new];
25+
_alertWindow.windowLevel = UIWindowLevelAlert + 1;
26+
}
27+
return _alertWindow;
28+
}
29+
30+
- (void)show:(BOOL)animated completion:(void (^)(void))completion
31+
{
32+
[self.alertWindow makeKeyAndVisible];
33+
[self.alertWindow.rootViewController presentViewController:self animated:animated completion:completion];
34+
}
35+
36+
@end

React/CoreModules/RCTAlertManager.mm

+6-25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import <React/RCTUtils.h>
1616

1717
#import "CoreModulesPlugins.h"
18+
#import "RCTAlertController.h"
1819

1920
@implementation RCTConvert (UIAlertViewStyle)
2021

@@ -99,29 +100,9 @@ - (void)invalidate
99100
}
100101
}
101102

102-
UIViewController *presentingController = RCTPresentedViewController();
103-
if (presentingController == nil) {
104-
RCTLogError(@"Tried to display alert view but there is no application window. args: %@", @{
105-
@"title" : args.title() ?: [NSNull null],
106-
@"message" : args.message() ?: [NSNull null],
107-
@"buttons" : RCTConvertOptionalVecToArray(
108-
args.buttons(),
109-
^id(id<NSObject> element) {
110-
return element;
111-
})
112-
?: [NSNull null],
113-
@"type" : args.type() ?: [NSNull null],
114-
@"defaultValue" : args.defaultValue() ?: [NSNull null],
115-
@"cancelButtonKey" : args.cancelButtonKey() ?: [NSNull null],
116-
@"destructiveButtonKey" : args.destructiveButtonKey() ?: [NSNull null],
117-
@"keyboardType" : args.keyboardType() ?: [NSNull null],
118-
});
119-
return;
120-
}
121-
122-
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
123-
message:nil
124-
preferredStyle:UIAlertControllerStyleAlert];
103+
RCTAlertController *alertController = [RCTAlertController alertControllerWithTitle:title
104+
message:nil
105+
preferredStyle:UIAlertControllerStyleAlert];
125106
switch (type) {
126107
case RCTAlertViewStylePlainTextInput: {
127108
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
@@ -170,7 +151,7 @@ - (void)invalidate
170151
} else if ([buttonKey isEqualToString:destructiveButtonKey]) {
171152
buttonStyle = UIAlertActionStyleDestructive;
172153
}
173-
__weak UIAlertController *weakAlertController = alertController;
154+
__weak RCTAlertController *weakAlertController = alertController;
174155
[alertController
175156
addAction:[UIAlertAction
176157
actionWithTitle:buttonTitle
@@ -202,7 +183,7 @@ - (void)invalidate
202183
[_alertControllers addObject:alertController];
203184

204185
dispatch_async(dispatch_get_main_queue(), ^{
205-
[presentingController presentViewController:alertController animated:YES completion:nil];
186+
[alertController show:YES completion:nil];
206187
});
207188
}
208189

0 commit comments

Comments
 (0)