|
23 | 23 |
|
24 | 24 | #import "PFLocalization.h"
|
25 | 25 |
|
| 26 | +@interface PFUIAlertView () <UIAlertViewDelegate> |
| 27 | + |
| 28 | +@property (nonatomic, copy) PFUIAlertViewCompletion completion; |
| 29 | + |
| 30 | +@end |
| 31 | + |
26 | 32 | @implementation PFUIAlertView
|
27 | 33 |
|
28 |
| -+ (void)showAlertViewWithTitle:(NSString *)title error:(NSError *)error { |
| 34 | +///-------------------------------------- |
| 35 | +#pragma mark - Present |
| 36 | +///-------------------------------------- |
| 37 | + |
| 38 | ++ (void)presentAlertInViewController:(UIViewController *)viewController |
| 39 | + withTitle:(NSString *)title |
| 40 | + message:(nullable NSString *)message |
| 41 | + cancelButtonTitle:(NSString *)cancelButtonTitle |
| 42 | + otherButtonTitles:(nullable NSArray *)otherButtonTitles |
| 43 | + completion:(nullable PFUIAlertViewCompletion)completion { |
| 44 | + if ([UIAlertController class] != nil) { |
| 45 | + __block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title |
| 46 | + message:message |
| 47 | + preferredStyle:UIAlertControllerStyleAlert]; |
| 48 | + |
| 49 | + void (^alertActionHandler)(UIAlertAction *) = [^(UIAlertAction *action) { |
| 50 | + if (completion) { |
| 51 | + // This block intentionally retains alertController, and releases it afterwards. |
| 52 | + if (action.style == UIAlertActionStyleCancel) { |
| 53 | + completion(NSNotFound); |
| 54 | + } else { |
| 55 | + NSUInteger index = [alertController.actions indexOfObject:action]; |
| 56 | + completion(index - 1); |
| 57 | + } |
| 58 | + } |
| 59 | + alertController = nil; |
| 60 | + } copy]; |
| 61 | + |
| 62 | + [alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle |
| 63 | + style:UIAlertActionStyleCancel |
| 64 | + handler:alertActionHandler]]; |
| 65 | + |
| 66 | + for (NSString *buttonTitle in otherButtonTitles) { |
| 67 | + [alertController addAction:[UIAlertAction actionWithTitle:buttonTitle |
| 68 | + style:UIAlertActionStyleDefault |
| 69 | + handler:alertActionHandler]]; |
| 70 | + } |
| 71 | + |
| 72 | + [viewController presentViewController:alertController animated:YES completion:nil]; |
| 73 | + } else { |
| 74 | +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 |
| 75 | + __block PFUIAlertView *pfAlertView = [[self alloc] init]; |
| 76 | + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title |
| 77 | + message:message |
| 78 | + delegate:nil |
| 79 | + cancelButtonTitle:cancelButtonTitle |
| 80 | + otherButtonTitles:nil]; |
| 81 | + |
| 82 | + for (NSString *buttonTitle in otherButtonTitles) { |
| 83 | + [alertView addButtonWithTitle:buttonTitle]; |
| 84 | + } |
| 85 | + |
| 86 | + pfAlertView.completion = ^(NSUInteger index) { |
| 87 | + if (completion) { |
| 88 | + completion(index); |
| 89 | + } |
| 90 | + |
| 91 | + pfAlertView = nil; |
| 92 | + }; |
| 93 | + |
| 94 | + alertView.delegate = pfAlertView; |
| 95 | + [alertView show]; |
| 96 | +#endif |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | ++ (void)presentAlertInViewController:(UIViewController *)viewController |
| 101 | + withTitle:(NSString *)title |
| 102 | + message:(nullable NSString *)message |
| 103 | + textFieldCustomizationHandler:(PFUIAlertViewTextFieldCustomizationHandler)textFieldCustomizationHandler |
| 104 | + cancelButtonTitle:(NSString *)cancelButtonTitle |
| 105 | + otherButtonTitles:(nullable NSArray *)otherButtonTitles |
| 106 | + completion:(nullable PFUIAlertViewTextFieldCompletion)completion { |
| 107 | + if ([UIAlertController class] != nil) { |
| 108 | + __block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title |
| 109 | + message:message |
| 110 | + preferredStyle:UIAlertControllerStyleAlert]; |
| 111 | + [alertController addTextFieldWithConfigurationHandler:textFieldCustomizationHandler]; |
| 112 | + void (^alertActionHandler)(UIAlertAction *) = [^(UIAlertAction *action) { |
| 113 | + if (completion) { |
| 114 | + UITextField *textField = alertController.textFields.firstObject; |
| 115 | + // This block intentionally retains alertController, and releases it afterwards. |
| 116 | + if (action.style == UIAlertActionStyleCancel) { |
| 117 | + completion(textField, NSNotFound); |
| 118 | + } else { |
| 119 | + NSUInteger index = [alertController.actions indexOfObject:action]; |
| 120 | + completion(textField, index - 1); |
| 121 | + } |
| 122 | + } |
| 123 | + alertController = nil; |
| 124 | + } copy]; |
| 125 | + |
| 126 | + [alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle |
| 127 | + style:UIAlertActionStyleCancel |
| 128 | + handler:alertActionHandler]]; |
| 129 | + |
| 130 | + for (NSString *buttonTitle in otherButtonTitles) { |
| 131 | + [alertController addAction:[UIAlertAction actionWithTitle:buttonTitle |
| 132 | + style:UIAlertActionStyleDefault |
| 133 | + handler:alertActionHandler]]; |
| 134 | + } |
| 135 | + |
| 136 | + [viewController presentViewController:alertController animated:YES completion:nil]; |
| 137 | + } else { |
| 138 | +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 |
| 139 | + __block PFUIAlertView *pfAlertView = [[self alloc] init]; |
| 140 | + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title |
| 141 | + message:message |
| 142 | + delegate:nil |
| 143 | + cancelButtonTitle:cancelButtonTitle |
| 144 | + otherButtonTitles:nil]; |
| 145 | + alertView.alertViewStyle = UIAlertViewStylePlainTextInput; |
| 146 | + for (NSString *buttonTitle in otherButtonTitles) { |
| 147 | + [alertView addButtonWithTitle:buttonTitle]; |
| 148 | + } |
| 149 | + textFieldCustomizationHandler([alertView textFieldAtIndex:0]); |
| 150 | + |
| 151 | + __weak UIAlertView *walertView = alertView; |
| 152 | + pfAlertView.completion = ^(NSUInteger index) { |
| 153 | + if (completion) { |
| 154 | + UITextField *textField = [walertView textFieldAtIndex:0]; |
| 155 | + completion(textField, index); |
| 156 | + } |
| 157 | + |
| 158 | + pfAlertView = nil; |
| 159 | + }; |
| 160 | + |
| 161 | + alertView.delegate = pfAlertView; |
| 162 | + [alertView show]; |
| 163 | +#endif |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +///-------------------------------------- |
| 168 | +#pragma mark - Convenience |
| 169 | +///-------------------------------------- |
| 170 | + |
| 171 | ++ (void)presentAlertInViewController:(UIViewController *)viewController |
| 172 | + withTitle:(NSString *)title |
| 173 | + error:(NSError *)error { |
29 | 174 | NSString *message = error.userInfo[@"error"];
|
30 | 175 | if (!message) {
|
31 |
| - message = [error.userInfo[@"originalError"] localizedDescription]; |
| 176 | + message = [error.userInfo[@"originalError"] localizedDescription]; |
32 | 177 | }
|
33 | 178 | if (!message) {
|
34 |
| - message = [error localizedDescription]; |
| 179 | + message = [error localizedDescription]; |
35 | 180 | }
|
36 |
| - [self showAlertViewWithTitle:title message:message]; |
| 181 | + [self presentAlertInViewController:viewController withTitle:title message:message]; |
37 | 182 | }
|
38 | 183 |
|
39 |
| -+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message { |
40 |
| - [self showAlertViewWithTitle:title |
41 |
| - message:message |
42 |
| - cancelButtonTitle:PFLocalizedString(@"OK", @"OK")]; |
| 184 | ++ (void)presentAlertInViewController:(UIViewController *)viewController |
| 185 | + withTitle:(NSString *)title |
| 186 | + message:(nullable NSString *)message { |
| 187 | + [self presentAlertInViewController:viewController |
| 188 | + withTitle:title |
| 189 | + message:message |
| 190 | + cancelButtonTitle:PFLocalizedString(@"OK", @"OK") |
| 191 | + otherButtonTitles:nil |
| 192 | + completion:nil]; |
43 | 193 | }
|
44 | 194 |
|
45 |
| -+ (void)showAlertViewWithTitle:(NSString *)title |
46 |
| - message:(NSString *)message |
47 |
| - cancelButtonTitle:(NSString *)cancelButtonTitle { |
48 |
| - UIAlertView *alertView = [[self alloc] initWithTitle:title |
49 |
| - message:message |
50 |
| - delegate:nil |
51 |
| - cancelButtonTitle:cancelButtonTitle |
52 |
| - otherButtonTitles:nil]; |
53 |
| - [alertView show]; |
| 195 | +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 |
| 196 | + |
| 197 | +///-------------------------------------- |
| 198 | +#pragma mark - UIAlertViewDelegate |
| 199 | +///-------------------------------------- |
| 200 | + |
| 201 | +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 202 | + if (self.completion) { |
| 203 | + if (buttonIndex == alertView.cancelButtonIndex) { |
| 204 | + self.completion(NSNotFound); |
| 205 | + } else { |
| 206 | + self.completion(buttonIndex - 1); |
| 207 | + } |
| 208 | + } |
54 | 209 | }
|
55 | 210 |
|
| 211 | +#endif |
| 212 | + |
56 | 213 | @end
|
0 commit comments