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

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

View File

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

View File

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

View File

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