ActionSheetIOS - support share sheet on modals

Summary:
Fixes #6913 - follow up to this commit 43dcdaffe2 to support this in the 2nd method as well.
Closes https://github.com/facebook/react-native/pull/7244

Differential Revision: D3330843

fbshipit-source-id: 0923440550a7635202158b4afaba87e12f1c1d54
This commit is contained in:
Mike Grabowski 2016-05-20 17:03:34 -07:00 committed by Facebook Github Bot 1
parent 807726bcb4
commit 4446e31b5d
5 changed files with 25 additions and 16 deletions

View File

@ -66,10 +66,7 @@ RCT_EXPORT_METHOD(showActionSheetWithOptions:(NSDictionary *)options
NSInteger destructiveButtonIndex = options[@"destructiveButtonIndex"] ? [RCTConvert NSInteger:options[@"destructiveButtonIndex"]] : -1;
NSInteger cancelButtonIndex = options[@"cancelButtonIndex"] ? [RCTConvert NSInteger:options[@"cancelButtonIndex"]] : -1;
UIViewController *controller = RCTKeyWindow().rootViewController;
while (controller.presentedViewController) {
controller = controller.presentedViewController;
}
UIViewController *controller = RCTPresentedViewController();
if (controller == nil) {
RCTLogError(@"Tried to display action sheet but there is no application window. options: %@", options);
@ -195,7 +192,7 @@ RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
shareController.excludedActivityTypes = excludedActivityTypes;
}
UIViewController *controller = RCTKeyWindow().rootViewController;
UIViewController *controller = RCTPresentedViewController();
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0

View File

@ -150,7 +150,7 @@ didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info
[_pickerCallbacks addObject:callback];
[_pickerCancelCallbacks addObject:cancelCallback];
UIViewController *rootViewController = RCTKeyWindow().rootViewController;
UIViewController *rootViewController = RCTPresentedViewController();
[rootViewController presentViewController:imagePicker animated:YES completion:nil];
}
@ -164,7 +164,7 @@ didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info
[_pickerCallbacks removeObjectAtIndex:index];
[_pickerCancelCallbacks removeObjectAtIndex:index];
UIViewController *rootViewController = RCTKeyWindow().rootViewController;
UIViewController *rootViewController = RCTPresentedViewController();
[rootViewController dismissViewControllerAnimated:YES completion:nil];
if (args) {

View File

@ -72,9 +72,13 @@ RCT_EXTERN BOOL RCTRunningInAppExtension(void);
RCT_EXTERN UIApplication *__nullable RCTSharedApplication(void);
// Returns the current main window, useful if you need to access the root view
// or view controller, e.g. to present a modal view controller or alert.
// or view controller
RCT_EXTERN UIWindow *__nullable RCTKeyWindow(void);
// Returns the presented view controller, useful if you need
// e.g. to present a modal view controller or alert over it
RCT_EXTERN UIViewController *__nullable RCTPresentedViewController(void);
// Does this device support force touch (aka 3D Touch)?
RCT_EXTERN BOOL RCTForceTouchAvailable(void);

View File

@ -439,6 +439,21 @@ UIWindow *__nullable RCTKeyWindow(void)
return RCTSharedApplication().keyWindow;
}
UIViewController *__nullable RCTPresentedViewController(void)
{
if (RCTRunningInAppExtension()) {
return nil;
}
UIViewController *controller = RCTKeyWindow().rootViewController;
while (controller.presentedViewController) {
controller = controller.presentedViewController;
}
return controller;
}
BOOL RCTForceTouchAvailable(void)
{
static BOOL forceSupported;

View File

@ -150,19 +150,12 @@ RCT_EXPORT_METHOD(alertWithArgs:(NSDictionary *)args
#endif
{
UIViewController *presentingController = RCTKeyWindow().rootViewController;
UIViewController *presentingController = RCTPresentedViewController();
if (presentingController == nil) {
RCTLogError(@"Tried to display alert view but there is no application window. args: %@", args);
return;
}
// Walk the chain up to get the topmost modal view controller. If modals are
// presented the root view controller's view might not be in the window
// hierarchy, and presenting from it will fail.
while (presentingController.presentedViewController) {
presentingController = presentingController.presentedViewController;
}
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:title
message:nil