Skip to content

Commit 8935d6e

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Fix action sheet callback invoked more than once on iPad (#33099)
Summary: iOS will sometimes invoke the UIAlertAction handler for the cancel button more than once on iPad. This can be reproduced relatively easily by having a button that opens an action sheet and spam tapping outside the action sheet while it is opening. Since native module callbacks can only be invoked once this causes the app to crash here https://github.com/facebook/react-native/blob/main/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModule.mm#L206. ## Changelog [iOS] [Fixed] - Fix action sheet callback invoked more than once on iPad Pull Request resolved: #33099 Test Plan: Tested on iPad simulator to reproduce the crash and verified that this fixes it. Reviewed By: philIip Differential Revision: D34215327 Pulled By: ShikaSD fbshipit-source-id: 6f406e4df737a57e6dd702dd54260aa72eab31d6
1 parent c1a55c7 commit 8935d6e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

React/CoreModules/RCTActionSheetManager.mm

+8-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ - (void)presentViewController:(UIViewController *)alertController
118118

119119
NSInteger index = 0;
120120
bool isCancelButtonIndex = false;
121+
// The handler for a button might get called more than once when tapping outside
122+
// the action sheet on iPad. RCTResponseSenderBlock can only be called once so
123+
// keep track of callback invocation here.
124+
__block bool callbackInvoked = false;
121125
for (NSString *option in buttons) {
122126
UIAlertActionStyle style = UIAlertActionStyleDefault;
123127
if ([destructiveButtonIndices containsObject:@(index)]) {
@@ -131,7 +135,10 @@ - (void)presentViewController:(UIViewController *)alertController
131135
UIAlertAction *actionButton = [UIAlertAction actionWithTitle:option
132136
style:style
133137
handler:^(__unused UIAlertAction *action) {
134-
callback(@[ @(localIndex) ]);
138+
if (!callbackInvoked) {
139+
callbackInvoked = true;
140+
callback(@[ @(localIndex) ]);
141+
}
135142
}];
136143
if (isCancelButtonIndex) {
137144
[actionButton setValue:cancelButtonTintColor forKey:@"titleTextColor"];

0 commit comments

Comments
 (0)