RCTAlertManager.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTAlertManager.h"
  8. #import "RCTAssert.h"
  9. #import "RCTConvert.h"
  10. #import "RCTLog.h"
  11. #import "RCTUtils.h"
  12. @implementation RCTConvert (UIAlertViewStyle)
  13. RCT_ENUM_CONVERTER(RCTAlertViewStyle, (@{
  14. @"default": @(RCTAlertViewStyleDefault),
  15. @"secure-text": @(RCTAlertViewStyleSecureTextInput),
  16. @"plain-text": @(RCTAlertViewStylePlainTextInput),
  17. @"login-password": @(RCTAlertViewStyleLoginAndPasswordInput),
  18. }), RCTAlertViewStyleDefault, integerValue)
  19. @end
  20. @interface RCTAlertManager()
  21. @end
  22. @implementation RCTAlertManager
  23. {
  24. NSHashTable *_alertControllers;
  25. }
  26. RCT_EXPORT_MODULE()
  27. - (dispatch_queue_t)methodQueue
  28. {
  29. return dispatch_get_main_queue();
  30. }
  31. - (void)invalidate
  32. {
  33. for (UIAlertController *alertController in _alertControllers) {
  34. [alertController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  35. }
  36. }
  37. /**
  38. * @param {NSDictionary} args Dictionary of the form
  39. *
  40. * @{
  41. * @"message": @"<Alert message>",
  42. * @"buttons": @[
  43. * @{@"<key1>": @"<title1>"},
  44. * @{@"<key2>": @"<title2>"},
  45. * ],
  46. * @"cancelButtonKey": @"<key2>",
  47. * }
  48. * The key from the `buttons` dictionary is passed back in the callback on click.
  49. * Buttons are displayed in the order they are specified.
  50. */
  51. RCT_EXPORT_METHOD(alertWithArgs:(NSDictionary *)args
  52. callback:(RCTResponseSenderBlock)callback)
  53. {
  54. NSString *title = [RCTConvert NSString:args[@"title"]];
  55. NSString *message = [RCTConvert NSString:args[@"message"]];
  56. RCTAlertViewStyle type = [RCTConvert RCTAlertViewStyle:args[@"type"]];
  57. NSArray<NSDictionary *> *buttons = [RCTConvert NSDictionaryArray:args[@"buttons"]];
  58. NSString *defaultValue = [RCTConvert NSString:args[@"defaultValue"]];
  59. NSString *cancelButtonKey = [RCTConvert NSString:args[@"cancelButtonKey"]];
  60. NSString *destructiveButtonKey = [RCTConvert NSString:args[@"destructiveButtonKey"]];
  61. UIKeyboardType keyboardType = [RCTConvert UIKeyboardType:args[@"keyboardType"]];
  62. if (!title && !message) {
  63. RCTLogError(@"Must specify either an alert title, or message, or both");
  64. return;
  65. }
  66. if (buttons.count == 0) {
  67. if (type == RCTAlertViewStyleDefault) {
  68. buttons = @[@{@"0": RCTUIKitLocalizedString(@"OK")}];
  69. cancelButtonKey = @"0";
  70. } else {
  71. buttons = @[
  72. @{@"0": RCTUIKitLocalizedString(@"OK")},
  73. @{@"1": RCTUIKitLocalizedString(@"Cancel")},
  74. ];
  75. cancelButtonKey = @"1";
  76. }
  77. }
  78. UIViewController *presentingController = RCTPresentedViewController();
  79. if (presentingController == nil) {
  80. RCTLogError(@"Tried to display alert view but there is no application window. args: %@", args);
  81. return;
  82. }
  83. UIAlertController *alertController = [UIAlertController
  84. alertControllerWithTitle:title
  85. message:nil
  86. preferredStyle:UIAlertControllerStyleAlert];
  87. switch (type) {
  88. case RCTAlertViewStylePlainTextInput: {
  89. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  90. textField.secureTextEntry = NO;
  91. textField.text = defaultValue;
  92. textField.keyboardType = keyboardType;
  93. }];
  94. break;
  95. }
  96. case RCTAlertViewStyleSecureTextInput: {
  97. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  98. textField.placeholder = RCTUIKitLocalizedString(@"Password");
  99. textField.secureTextEntry = YES;
  100. textField.text = defaultValue;
  101. textField.keyboardType = keyboardType;
  102. }];
  103. break;
  104. }
  105. case RCTAlertViewStyleLoginAndPasswordInput: {
  106. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  107. textField.placeholder = RCTUIKitLocalizedString(@"Login");
  108. textField.text = defaultValue;
  109. textField.keyboardType = keyboardType;
  110. }];
  111. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  112. textField.placeholder = RCTUIKitLocalizedString(@"Password");
  113. textField.secureTextEntry = YES;
  114. }];
  115. break;
  116. }
  117. case RCTAlertViewStyleDefault:
  118. break;
  119. }
  120. alertController.message = message;
  121. for (NSDictionary<NSString *, id> *button in buttons) {
  122. if (button.count != 1) {
  123. RCTLogError(@"Button definitions should have exactly one key.");
  124. }
  125. NSString *buttonKey = button.allKeys.firstObject;
  126. NSString *buttonTitle = [RCTConvert NSString:button[buttonKey]];
  127. UIAlertActionStyle buttonStyle = UIAlertActionStyleDefault;
  128. if ([buttonKey isEqualToString:cancelButtonKey]) {
  129. buttonStyle = UIAlertActionStyleCancel;
  130. } else if ([buttonKey isEqualToString:destructiveButtonKey]) {
  131. buttonStyle = UIAlertActionStyleDestructive;
  132. }
  133. __weak UIAlertController *weakAlertController = alertController;
  134. [alertController addAction:[UIAlertAction actionWithTitle:buttonTitle
  135. style:buttonStyle
  136. handler:^(__unused UIAlertAction *action) {
  137. switch (type) {
  138. case RCTAlertViewStylePlainTextInput:
  139. case RCTAlertViewStyleSecureTextInput:
  140. callback(@[buttonKey, [weakAlertController.textFields.firstObject text]]);
  141. break;
  142. case RCTAlertViewStyleLoginAndPasswordInput: {
  143. NSDictionary<NSString *, NSString *> *loginCredentials = @{
  144. @"login": [weakAlertController.textFields.firstObject text],
  145. @"password": [weakAlertController.textFields.lastObject text]
  146. };
  147. callback(@[buttonKey, loginCredentials]);
  148. break;
  149. }
  150. case RCTAlertViewStyleDefault:
  151. callback(@[buttonKey]);
  152. break;
  153. }
  154. }]];
  155. }
  156. if (!_alertControllers) {
  157. _alertControllers = [NSHashTable weakObjectsHashTable];
  158. }
  159. [_alertControllers addObject:alertController];
  160. dispatch_async(dispatch_get_main_queue(), ^{
  161. [presentingController presentViewController:alertController animated:YES completion:nil];
  162. });
  163. }
  164. @end