Skip to content

Commit b1c8219

Browse files
committed
Address review: weak self, shield for share sheet, iPhone only
- Break the self -> handler -> self cycle with a weak reference. - Remove an old shield before presenting, and add the shield in the same place for both sheets. - Anchored share sheets get the shield too; an outside tap reports completed: false once. - Keep the shield while the share sheet stays open after backing out of an activity. - Add the shield to the presenting window, iPhone only; iPad keeps UIKit's popover behaviour. - Hide the shield from accessibility.
1 parent a532b00 commit b1c8219

1 file changed

Lines changed: 51 additions & 19 deletions

File tree

‎packages/react-native/React/CoreModules/RCTActionSheetManager.mm‎

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ - (void)showOutsideTouchShieldInWindow:(UIWindow *)window handler:(dispatch_bloc
6363
{
6464
[self hideOutsideTouchShield];
6565
UIControl *shield = [[UIControl alloc] initWithFrame:window.bounds];
66+
shield.isAccessibilityElement = NO;
6667
shield.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
6768
[shield addTarget:self action:@selector(outsideTouchShieldTouched) forControlEvents:UIControlEventTouchDown];
6869
[window addSubview:shield];
@@ -88,7 +89,9 @@ - (void)hideOutsideTouchShield
8889
- (void)presentViewController:(UIViewController *)alertController
8990
onParentViewController:(UIViewController *)parentViewController
9091
anchorViewTag:(NSNumber *)anchorViewTag
92+
outsideTouchHandler:(dispatch_block_t)outsideTouchHandler
9193
{
94+
[self hideOutsideTouchShield];
9295
alertController.modalPresentationStyle = UIModalPresentationPopover;
9396
UIView *sourceView = parentViewController.view;
9497
UIView *anchorView = [self anchorViewForTag:anchorViewTag];
@@ -113,6 +116,10 @@ - (void)presentViewController:(UIViewController *)alertController
113116
alertController.popoverPresentationController.sourceView = sourceView;
114117
alertController.popoverPresentationController.sourceRect = sourceView.bounds;
115118
}
119+
// iPad keeps its popover behaviour, so the shield is only added on iPhone.
120+
if (presentsFromAnchor && [UIDevice.currentDevice userInterfaceIdiom] != UIUserInterfaceIdiomPad) {
121+
[self showOutsideTouchShieldInWindow:parentViewController.view.window handler:outsideTouchHandler];
122+
}
116123
[parentViewController presentViewController:alertController animated:YES completion:nil];
117124
}
118125

@@ -256,24 +263,24 @@ - (void)showActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowAction
256263

257264
[self->_alertControllers addObject:alertController];
258265

259-
UIView *anchorView = nil;
260-
if (@available(iOS 26.0, *)) {
261-
anchorView = [self anchorViewForTag:anchorViewTag];
262-
}
263-
if (anchorView != nil) {
264-
[self showOutsideTouchShieldInWindow:anchorView.window
265-
handler:^{
266-
[alertController dismissViewControllerAnimated:YES completion:nil];
267-
[self->_alertControllers removeObject:alertController];
268-
[self hideOutsideTouchShield];
269-
// As in UIKit, a tap outside the popover selects the cancel button, if any.
270-
if (!callbackInvoked && cancelButtonIndex >= 0) {
271-
callbackInvoked = true;
272-
callback(@[ @(cancelButtonIndex) ]);
273-
}
274-
}];
275-
}
276-
[self presentViewController:alertController onParentViewController:controller anchorViewTag:anchorViewTag];
266+
__weak RCTActionSheetManager *weakSelf = self;
267+
[self presentViewController:alertController
268+
onParentViewController:controller
269+
anchorViewTag:anchorViewTag
270+
outsideTouchHandler:^{
271+
RCTActionSheetManager *strongSelf = weakSelf;
272+
if (strongSelf == nil) {
273+
return;
274+
}
275+
[alertController dismissViewControllerAnimated:YES completion:nil];
276+
[strongSelf->_alertControllers removeObject:alertController];
277+
[strongSelf hideOutsideTouchShield];
278+
// As in UIKit, a tap outside the popover selects the cancel button, if any.
279+
if (!callbackInvoked && cancelButtonIndex >= 0) {
280+
callbackInvoked = true;
281+
callback(@[ @(cancelButtonIndex) ]);
282+
}
283+
}];
277284
});
278285
}
279286

@@ -345,8 +352,30 @@ - (void)showShareActionSheetWithOptions:
345352
}
346353

347354
UIViewController *controller = RCTPresentedViewController();
355+
__weak RCTActionSheetManager *weakSelf = self;
356+
__weak UIActivityViewController *weakShareController = shareController;
357+
__block bool callbackInvoked = false;
358+
dispatch_block_t outsideTouchHandler = [^{
359+
[weakSelf hideOutsideTouchShield];
360+
[weakShareController dismissViewControllerAnimated:YES completion:nil];
361+
// Same result as closing the share sheet: not completed, no activity.
362+
if (!callbackInvoked) {
363+
callbackInvoked = true;
364+
successCallback(@[ @NO, [NSNull null] ]);
365+
}
366+
} copy];
348367
shareController.completionWithItemsHandler =
349368
^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) {
369+
// Backing out of an activity calls this with the share sheet still open, so the shield stays.
370+
// A later sheet may have its own shield by now, so only this sheet's shield is removed.
371+
RCTActionSheetManager *strongSelf = weakSelf;
372+
if ((activityError != nullptr || completed || activityType == nil) &&
373+
strongSelf.outsideTouchHandler == outsideTouchHandler) {
374+
[strongSelf hideOutsideTouchShield];
375+
}
376+
if (callbackInvoked) {
377+
return;
378+
}
350379
if (activityError != nullptr) {
351380
failureCallback(@[ RCTJSErrorFromNSError(activityError) ]);
352381
} else if (completed || activityType == nil) {
@@ -364,7 +393,10 @@ - (void)showShareActionSheetWithOptions:
364393
shareController.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
365394
}
366395

367-
[self presentViewController:shareController onParentViewController:controller anchorViewTag:anchorViewTag];
396+
[self presentViewController:shareController
397+
onParentViewController:controller
398+
anchorViewTag:anchorViewTag
399+
outsideTouchHandler:outsideTouchHandler];
368400
});
369401
}
370402

0 commit comments

Comments
 (0)