Fixed display of alerts on top of modal window

Reviewed By: javache

Differential Revision: D2616170

fb-gh-sync-id: f72f728008099fff6fc966d7a2ce4c0d27a4fefd
This commit is contained in:
Nick Lockwood 2015-11-05 03:49:41 -08:00 committed by facebook-github-bot-5
parent 0da2004e88
commit 11df4cb08c
6 changed files with 29 additions and 22 deletions

View File

@ -66,7 +66,7 @@ RCT_EXPORT_METHOD(showActionSheetWithOptions:(NSDictionary *)options
NSInteger destructiveButtonIndex = options[@"destructiveButtonIndex"] ? [RCTConvert NSInteger:options[@"destructiveButtonIndex"]] : -1; NSInteger destructiveButtonIndex = options[@"destructiveButtonIndex"] ? [RCTConvert NSInteger:options[@"destructiveButtonIndex"]] : -1;
NSInteger cancelButtonIndex = options[@"cancelButtonIndex"] ? [RCTConvert NSInteger:options[@"cancelButtonIndex"]] : -1; NSInteger cancelButtonIndex = options[@"cancelButtonIndex"] ? [RCTConvert NSInteger:options[@"cancelButtonIndex"]] : -1;
UIViewController *controller = RCTSharedApplication().delegate.window.rootViewController; UIViewController *controller = RCTKeyWindow().rootViewController;
if (controller == nil) { if (controller == nil) {
RCTLogError(@"Tried to display action sheet but there is no application window. options: %@", options); RCTLogError(@"Tried to display action sheet but there is no application window. options: %@", options);
return; return;
@ -166,7 +166,7 @@ RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
} }
UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil]; UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
UIViewController *controller = RCTSharedApplication().delegate.window.rootViewController; UIViewController *controller = RCTKeyWindow().rootViewController;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
@ -212,8 +212,6 @@ RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
} else { } else {
RCTLogWarn(@"No callback registered for action sheet: %@", actionSheet.title); RCTLogWarn(@"No callback registered for action sheet: %@", actionSheet.title);
} }
[RCTSharedApplication().delegate.window makeKeyWindow];
} }
@end @end

View File

@ -60,8 +60,7 @@ RCT_EXPORT_METHOD(openCameraDialog:(NSDictionary *)config
return; return;
} }
UIWindow *keyWindow = RCTSharedApplication().keyWindow; UIViewController *rootViewController = RCTKeyWindow().rootViewController;
UIViewController *rootViewController = keyWindow.rootViewController;
UIImagePickerController *imagePicker = [UIImagePickerController new]; UIImagePickerController *imagePicker = [UIImagePickerController new];
imagePicker.delegate = self; imagePicker.delegate = self;
@ -87,8 +86,7 @@ RCT_EXPORT_METHOD(openSelectDialog:(NSDictionary *)config
return; return;
} }
UIWindow *keyWindow = RCTSharedApplication().keyWindow; UIViewController *rootViewController = RCTKeyWindow().rootViewController;
UIViewController *rootViewController = keyWindow.rootViewController;
UIImagePickerController *imagePicker = [UIImagePickerController new]; UIImagePickerController *imagePicker = [UIImagePickerController new];
imagePicker.delegate = self; imagePicker.delegate = self;
@ -121,8 +119,7 @@ didFinishPickingMediaWithInfo:(NSDictionary *)info
[_pickerCallbacks removeObjectAtIndex:index]; [_pickerCallbacks removeObjectAtIndex:index];
[_pickerCancelCallbacks removeObjectAtIndex:index]; [_pickerCancelCallbacks removeObjectAtIndex:index];
UIWindow *keyWindow = RCTSharedApplication().keyWindow; UIViewController *rootViewController = RCTKeyWindow().rootViewController;
UIViewController *rootViewController = keyWindow.rootViewController;
[rootViewController dismissViewControllerAnimated:YES completion:nil]; [rootViewController dismissViewControllerAnimated:YES completion:nil];
callback(@[[info[UIImagePickerControllerReferenceURL] absoluteString]]); callback(@[[info[UIImagePickerControllerReferenceURL] absoluteString]]);
@ -137,8 +134,7 @@ didFinishPickingMediaWithInfo:(NSDictionary *)info
[_pickerCallbacks removeObjectAtIndex:index]; [_pickerCallbacks removeObjectAtIndex:index];
[_pickerCancelCallbacks removeObjectAtIndex:index]; [_pickerCancelCallbacks removeObjectAtIndex:index];
UIWindow *keyWindow = RCTSharedApplication().keyWindow; UIViewController *rootViewController = RCTKeyWindow().rootViewController;
UIViewController *rootViewController = keyWindow.rootViewController;
[rootViewController dismissViewControllerAnimated:YES completion:nil]; [rootViewController dismissViewControllerAnimated:YES completion:nil];
callback(@[]); callback(@[]);

View File

@ -58,6 +58,10 @@ RCT_EXTERN BOOL RCTRunningInAppExtension(void);
// Returns the shared UIApplication instance, or nil if running in an App Extension // Returns the shared UIApplication instance, or nil if running in an App Extension
RCT_EXTERN UIApplication *RCTSharedApplication(void); RCT_EXTERN UIApplication *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.
RCT_EXTERN UIWindow *RCTKeyWindow(void);
// Return a UIAlertView initialized with the given values // Return a UIAlertView initialized with the given values
// or nil if running in an app extension // or nil if running in an app extension
RCT_EXTERN UIAlertView *RCTAlertView(NSString *title, RCT_EXTERN UIAlertView *RCTAlertView(NSString *title,

View File

@ -342,26 +342,35 @@ BOOL RCTRunningInAppExtension(void)
return [[[[NSBundle mainBundle] bundlePath] pathExtension] isEqualToString:@"appex"]; return [[[[NSBundle mainBundle] bundlePath] pathExtension] isEqualToString:@"appex"];
} }
id RCTSharedApplication(void) UIApplication *RCTSharedApplication(void)
{ {
if (RCTRunningInAppExtension()) { if (RCTRunningInAppExtension()) {
return nil; return nil;
} }
return [[UIApplication class] performSelector:@selector(sharedApplication)]; return [[UIApplication class] performSelector:@selector(sharedApplication)];
} }
id RCTAlertView(NSString *title, UIWindow *RCTKeyWindow(void)
NSString *message, {
id delegate, if (RCTRunningInAppExtension()) {
NSString *cancelButtonTitle, return nil;
NSArray<NSString *> *otherButtonTitles) }
// TODO: replace with a more robust solution
return RCTSharedApplication().keyWindow;
}
UIAlertView *RCTAlertView(NSString *title,
NSString *message,
id delegate,
NSString *cancelButtonTitle,
NSArray<NSString *> *otherButtonTitles)
{ {
if (RCTRunningInAppExtension()) { if (RCTRunningInAppExtension()) {
RCTLogError(@"RCTAlertView is unavailable when running in an app extension"); RCTLogError(@"RCTAlertView is unavailable when running in an app extension");
return nil; return nil;
} }
UIAlertView *alertView = [UIAlertView new]; UIAlertView *alertView = [UIAlertView new];
alertView.title = title; alertView.title = title;
alertView.message = message; alertView.message = message;

View File

@ -89,7 +89,7 @@ RCT_EXPORT_METHOD(alertWithArgs:(NSDictionary *)args
return; return;
} }
UIViewController *presentingController = RCTSharedApplication().delegate.window.rootViewController; UIViewController *presentingController = RCTKeyWindow().rootViewController;
if (presentingController == nil) { if (presentingController == nil) {
RCTLogError(@"Tried to display alert view but there is no application window. args: %@", args); RCTLogError(@"Tried to display alert view but there is no application window. args: %@", args);
return; return;

View File

@ -454,7 +454,7 @@ RCT_EXPORT_METHOD(show)
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1; actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
actionSheet.actionSheetStyle = UIBarStyleBlack; actionSheet.actionSheetStyle = UIBarStyleBlack;
[actionSheet showInView:RCTSharedApplication().keyWindow.rootViewController.view]; [actionSheet showInView:RCTKeyWindow().rootViewController.view];
_actionSheet = actionSheet; _actionSheet = actionSheet;
_presentedItems = items; _presentedItems = items;
} }