2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTAlertManager.h"
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
#import "RCTAssert.h"
|
2015-11-03 22:45:46 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
2015-09-22 17:43:56 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-12-01 02:44:06 +00:00
|
|
|
@implementation RCTConvert (UIAlertViewStyle)
|
|
|
|
|
|
|
|
RCT_ENUM_CONVERTER(UIAlertViewStyle, (@{
|
|
|
|
@"default": @(UIAlertViewStyleDefault),
|
|
|
|
@"secure-text": @(UIAlertViewStyleSecureTextInput),
|
|
|
|
@"plain-text": @(UIAlertViewStylePlainTextInput),
|
|
|
|
@"login-password": @(UIAlertViewStyleLoginAndPasswordInput),
|
|
|
|
}), UIAlertViewStyleDefault, integerValue)
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@interface RCTAlertManager() <UIAlertViewDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTAlertManager
|
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<UIAlertView *> *_alerts;
|
|
|
|
NSMutableArray<UIAlertController *> *_alertControllers;
|
|
|
|
NSMutableArray<RCTResponseSenderBlock> *_alertCallbacks;
|
|
|
|
NSMutableArray<NSArray<NSString *> *> *_alertButtonKeys;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-04-20 19:06:02 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:40:49 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
2015-10-30 18:23:47 +00:00
|
|
|
for (UIAlertView *alert in _alerts) {
|
|
|
|
[alert dismissWithClickedButtonIndex:0 animated:YES];
|
|
|
|
}
|
|
|
|
for (UIAlertController *alertController in _alertControllers) {
|
|
|
|
[alertController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
2015-09-18 15:40:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* @param {NSDictionary} args Dictionary of the form
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
* @"message": @"<Alert message>",
|
|
|
|
* @"buttons": @[
|
|
|
|
* @{@"<key1>": @"<title1>"},
|
2015-12-01 02:44:06 +00:00
|
|
|
* @{@"<key2>": @"<title2>"},
|
|
|
|
* ],
|
|
|
|
* @"cancelButtonKey": @"<key2>",
|
2015-02-20 04:10:52 +00:00
|
|
|
* }
|
|
|
|
* The key from the `buttons` dictionary is passed back in the callback on click.
|
2015-12-01 02:44:06 +00:00
|
|
|
* Buttons are displayed in the order they are specified.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(alertWithArgs:(NSDictionary *)args
|
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
NSString *title = [RCTConvert NSString:args[@"title"]];
|
|
|
|
NSString *message = [RCTConvert NSString:args[@"message"]];
|
2015-12-01 02:44:06 +00:00
|
|
|
UIAlertViewStyle type = [RCTConvert UIAlertViewStyle:args[@"type"]];
|
|
|
|
NSArray<NSDictionary *> *buttons = [RCTConvert NSDictionaryArray:args[@"buttons"]];
|
|
|
|
NSString *cancelButtonKey = [RCTConvert NSString:args[@"cancelButtonKey"]];
|
|
|
|
NSString *destructiveButtonKey = [RCTConvert NSString:args[@"destructiveButtonKey"]];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
if (!title && !message) {
|
2015-03-01 23:33:55 +00:00
|
|
|
RCTLogError(@"Must specify either an alert title, or message, or both");
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
2015-12-01 02:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buttons.count == 0) {
|
|
|
|
if (type == UIAlertViewStyleDefault) {
|
|
|
|
buttons = @[@{@"0": RCTUIKitLocalizedString(@"OK")}];
|
|
|
|
cancelButtonKey = @"0";
|
|
|
|
} else {
|
|
|
|
buttons = @[
|
|
|
|
@{@"0": RCTUIKitLocalizedString(@"OK")},
|
|
|
|
@{@"1": RCTUIKitLocalizedString(@"Cancel")},
|
|
|
|
];
|
|
|
|
cancelButtonKey = @"1";
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-10-30 18:23:47 +00:00
|
|
|
|
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
2015-11-03 22:45:46 +00:00
|
|
|
|
2015-12-01 02:44:06 +00:00
|
|
|
// TODO: we've encountered some bug when presenting alerts on top of a window
|
|
|
|
// that is subsequently dismissed. As a temporary solution to this, we'll use
|
|
|
|
// UIAlertView preferentially if it's available and supports our use case.
|
|
|
|
BOOL preferAlertView = (!RCTRunningInAppExtension() &&
|
|
|
|
!destructiveButtonKey &&
|
|
|
|
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
|
2015-11-16 18:20:34 +00:00
|
|
|
|
|
|
|
if (preferAlertView || [UIAlertController class] == nil) {
|
2015-11-03 22:45:46 +00:00
|
|
|
|
2015-10-30 18:23:47 +00:00
|
|
|
UIAlertView *alertView = RCTAlertView(title, nil, self, nil, nil);
|
2015-12-01 02:44:06 +00:00
|
|
|
alertView.alertViewStyle = type;
|
|
|
|
alertView.message = message;
|
2015-10-30 18:23:47 +00:00
|
|
|
|
2015-12-01 02:44:06 +00:00
|
|
|
NSMutableArray<NSString *> *buttonKeys =
|
|
|
|
[[NSMutableArray alloc] initWithCapacity:buttons.count];
|
2015-10-30 18:23:47 +00:00
|
|
|
|
|
|
|
NSInteger index = 0;
|
2015-12-01 02:44:06 +00:00
|
|
|
for (NSDictionary<NSString *, id> *button in buttons) {
|
2015-10-30 18:23:47 +00:00
|
|
|
if (button.count != 1) {
|
|
|
|
RCTLogError(@"Button definitions should have exactly one key.");
|
|
|
|
}
|
|
|
|
NSString *buttonKey = button.allKeys.firstObject;
|
2015-12-01 02:44:06 +00:00
|
|
|
NSString *buttonTitle = [RCTConvert NSString:button[buttonKey]];
|
2015-10-30 18:23:47 +00:00
|
|
|
[alertView addButtonWithTitle:buttonTitle];
|
2015-12-01 02:44:06 +00:00
|
|
|
if ([buttonKey isEqualToString:cancelButtonKey]) {
|
|
|
|
alertView.cancelButtonIndex = buttonKeys.count;
|
2015-10-30 18:23:47 +00:00
|
|
|
}
|
|
|
|
[buttonKeys addObject:buttonKey];
|
|
|
|
index ++;
|
|
|
|
}
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
if (!_alerts) {
|
|
|
|
_alerts = [NSMutableArray new];
|
|
|
|
_alertCallbacks = [NSMutableArray new];
|
|
|
|
_alertButtonKeys = [NSMutableArray new];
|
|
|
|
}
|
2015-10-30 18:23:47 +00:00
|
|
|
[_alerts addObject:alertView];
|
|
|
|
[_alertCallbacks addObject:callback ?: ^(__unused id unused) {}];
|
|
|
|
[_alertButtonKeys addObject:buttonKeys];
|
|
|
|
|
|
|
|
[alertView show];
|
2015-11-03 22:45:46 +00:00
|
|
|
|
2015-10-30 18:23:47 +00:00
|
|
|
} else
|
2015-11-03 22:45:46 +00:00
|
|
|
|
2015-10-30 18:23:47 +00:00
|
|
|
#endif
|
2015-11-03 22:45:46 +00:00
|
|
|
|
2015-10-30 18:23:47 +00:00
|
|
|
{
|
2015-11-16 18:20:34 +00:00
|
|
|
UIViewController *presentingController = RCTKeyWindow().rootViewController;
|
|
|
|
if (presentingController == nil) {
|
|
|
|
RCTLogError(@"Tried to display alert view but there is no application window. args: %@", args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-01 02:44:06 +00:00
|
|
|
// 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.
|
2015-11-16 18:20:34 +00:00
|
|
|
while (presentingController.presentedViewController) {
|
|
|
|
presentingController = presentingController.presentedViewController;
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:23:47 +00:00
|
|
|
UIAlertController *alertController =
|
2015-12-01 02:44:06 +00:00
|
|
|
[UIAlertController alertControllerWithTitle:title
|
|
|
|
message:nil
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
switch (type) {
|
|
|
|
case UIAlertViewStylePlainTextInput:
|
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.secureTextEntry = NO;
|
|
|
|
}];
|
|
|
|
break;
|
|
|
|
case UIAlertViewStyleSecureTextInput:
|
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.placeholder = RCTUIKitLocalizedString(@"Password");
|
|
|
|
textField.secureTextEntry = YES;
|
|
|
|
}];
|
|
|
|
break;
|
|
|
|
case UIAlertViewStyleLoginAndPasswordInput:
|
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.placeholder = RCTUIKitLocalizedString(@"Login");
|
|
|
|
}];
|
|
|
|
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.placeholder = RCTUIKitLocalizedString(@"Password");
|
|
|
|
textField.secureTextEntry = YES;
|
|
|
|
}];
|
|
|
|
case UIAlertViewStyleDefault:
|
|
|
|
break;
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-03-23 20:28:42 +00:00
|
|
|
|
2015-12-01 02:44:06 +00:00
|
|
|
alertController.message = message;
|
|
|
|
|
|
|
|
for (NSDictionary<NSString *, id> *button in buttons) {
|
2015-10-30 18:23:47 +00:00
|
|
|
if (button.count != 1) {
|
|
|
|
RCTLogError(@"Button definitions should have exactly one key.");
|
|
|
|
}
|
|
|
|
NSString *buttonKey = button.allKeys.firstObject;
|
2015-12-01 02:44:06 +00:00
|
|
|
NSString *buttonTitle = [RCTConvert NSString:button[buttonKey]];
|
|
|
|
UIAlertActionStyle buttonStyle = UIAlertActionStyleDefault;
|
|
|
|
if ([buttonKey isEqualToString:cancelButtonKey]) {
|
|
|
|
buttonStyle = UIAlertActionStyleCancel;
|
|
|
|
} else if ([buttonKey isEqualToString:destructiveButtonKey]) {
|
|
|
|
buttonStyle = UIAlertActionStyleDestructive;
|
|
|
|
}
|
2015-10-30 18:23:47 +00:00
|
|
|
[alertController addAction:[UIAlertAction actionWithTitle:buttonTitle
|
|
|
|
style:buttonStyle
|
2015-11-03 22:45:46 +00:00
|
|
|
handler:^(__unused UIAlertAction *action) {
|
2015-12-01 02:44:06 +00:00
|
|
|
switch (type) {
|
|
|
|
case UIAlertViewStylePlainTextInput:
|
|
|
|
case UIAlertViewStyleSecureTextInput:
|
|
|
|
callback(@[buttonKey, [alertController.textFields.firstObject text]]);
|
|
|
|
break;
|
|
|
|
case UIAlertViewStyleLoginAndPasswordInput: {
|
|
|
|
NSDictionary<NSString *, NSString *> *loginCredentials = @{
|
|
|
|
@"login": [alertController.textFields.firstObject text],
|
|
|
|
@"password": [alertController.textFields.lastObject text]
|
|
|
|
};
|
|
|
|
callback(@[buttonKey, loginCredentials]);
|
|
|
|
break;
|
2015-10-30 18:23:47 +00:00
|
|
|
}
|
2015-12-01 02:44:06 +00:00
|
|
|
case UIAlertViewStyleDefault:
|
|
|
|
callback(@[buttonKey]);
|
|
|
|
break;
|
2015-10-30 18:23:47 +00:00
|
|
|
}
|
|
|
|
}]];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
if (!_alertControllers) {
|
|
|
|
_alertControllers = [NSMutableArray new];
|
|
|
|
}
|
|
|
|
[_alertControllers addObject:alertController];
|
|
|
|
|
2015-10-30 18:23:47 +00:00
|
|
|
[presentingController presentViewController:alertController animated:YES completion:nil];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIAlertViewDelegate
|
|
|
|
|
|
|
|
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
|
|
|
|
{
|
|
|
|
NSUInteger index = [_alerts indexOfObject:alertView];
|
|
|
|
RCTAssert(index != NSNotFound, @"Dismissed alert was not recognised");
|
2015-03-23 20:28:42 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTResponseSenderBlock callback = _alertCallbacks[index];
|
2015-11-03 22:45:46 +00:00
|
|
|
NSArray<NSString *> *buttonKeys = _alertButtonKeys[index];
|
2015-05-11 23:40:04 +00:00
|
|
|
|
2015-12-01 02:44:06 +00:00
|
|
|
switch (alertView.alertViewStyle) {
|
|
|
|
case UIAlertViewStylePlainTextInput:
|
|
|
|
case UIAlertViewStyleSecureTextInput:
|
|
|
|
callback(@[buttonKeys[buttonIndex], [alertView textFieldAtIndex:0].text]);
|
|
|
|
break;
|
|
|
|
case UIAlertViewStyleLoginAndPasswordInput: {
|
|
|
|
NSDictionary<NSString *, NSString *> *loginCredentials = @{
|
|
|
|
@"login": [alertView textFieldAtIndex:0].text,
|
|
|
|
@"password": [alertView textFieldAtIndex:1].text,
|
|
|
|
};
|
|
|
|
callback(@[buttonKeys[buttonIndex], loginCredentials]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UIAlertViewStyleDefault:
|
|
|
|
callback(@[buttonKeys[buttonIndex]]);
|
|
|
|
break;
|
2015-05-11 23:40:04 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[_alerts removeObjectAtIndex:index];
|
|
|
|
[_alertCallbacks removeObjectAtIndex:index];
|
|
|
|
[_alertButtonKeys removeObjectAtIndex:index];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|