diff options
Diffstat (limited to 'iPhone/CordovaLib/Classes/CDVNotification.m')
-rwxr-xr-x | iPhone/CordovaLib/Classes/CDVNotification.m | 93 |
1 files changed, 57 insertions, 36 deletions
diff --git a/iPhone/CordovaLib/Classes/CDVNotification.m b/iPhone/CordovaLib/Classes/CDVNotification.m index 992239e..464eb1f 100755 --- a/iPhone/CordovaLib/Classes/CDVNotification.m +++ b/iPhone/CordovaLib/Classes/CDVNotification.m @@ -20,12 +20,25 @@ #import "CDVNotification.h" #import "NSDictionary+Extensions.h" +#define DIALOG_TYPE_ALERT @"alert" +#define DIALOG_TYPE_PROMPT @"prompt" + @implementation CDVNotification -- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSString*)buttons callbackId:(NSString*)callbackId +/* + * showDialogWithMessage - Common method to instantiate the alert view for alert, confirm, and prompt notifications. + * Parameters: + * message The alert view message. + * title The alert view title. + * buttons The array of customized strings for the buttons. + * defaultText The input text for the textbox (if textbox exists). + * callbackId The commmand callback id. + * dialogType The type of alert view [alert | prompt]. + */ +- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType { CDVAlertView* alertView = [[CDVAlertView alloc] - initWithTitle:title + initWithTitle:title message:message delegate:self cancelButtonTitle:nil @@ -33,11 +46,16 @@ alertView.callbackId = callbackId; - NSArray* labels = [buttons componentsSeparatedByString:@","]; - int count = [labels count]; + int count = [buttons count]; for (int n = 0; n < count; n++) { - [alertView addButtonWithTitle:[labels objectAtIndex:n]]; + [alertView addButtonWithTitle:[buttons objectAtIndex:n]]; + } + + if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) { + alertView.alertViewStyle = UIAlertViewStylePlainTextInput; + UITextField* textField = [alertView textFieldAtIndex:0]; + textField.text = defaultText; } [alertView show]; @@ -46,52 +64,55 @@ - (void)alert:(CDVInvokedUrlCommand*)command { NSString* callbackId = command.callbackId; - NSArray* arguments = command.arguments; - int argc = [arguments count]; - - NSString* message = argc > 0 ? [arguments objectAtIndex:0] : nil; - NSString* title = argc > 1 ? [arguments objectAtIndex:1] : nil; - NSString* buttons = argc > 2 ? [arguments objectAtIndex:2] : nil; - - if (!title) { - title = NSLocalizedString(@"Alert", @"Alert"); - } - if (!buttons) { - buttons = NSLocalizedString(@"OK", @"OK"); - } + NSString* message = [command argumentAtIndex:0]; + NSString* title = [command argumentAtIndex:1]; + NSString* buttons = [command argumentAtIndex:2]; - [self showDialogWithMessage:message title:title buttons:buttons callbackId:callbackId]; + [self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT]; } - (void)confirm:(CDVInvokedUrlCommand*)command { NSString* callbackId = command.callbackId; - NSArray* arguments = command.arguments; - int argc = [arguments count]; + NSString* message = [command argumentAtIndex:0]; + NSString* title = [command argumentAtIndex:1]; + NSArray* buttons = [command argumentAtIndex:2]; - NSString* message = argc > 0 ? [arguments objectAtIndex:0] : nil; - NSString* title = argc > 1 ? [arguments objectAtIndex:1] : nil; - NSString* buttons = argc > 2 ? [arguments objectAtIndex:2] : nil; + [self showDialogWithMessage:message title:title buttons:buttons defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT]; +} - if (!title) { - title = NSLocalizedString(@"Confirm", @"Confirm"); - } - if (!buttons) { - buttons = NSLocalizedString(@"OK,Cancel", @"OK,Cancel"); - } +- (void)prompt:(CDVInvokedUrlCommand*)command +{ + NSString* callbackId = command.callbackId; + NSString* message = [command argumentAtIndex:0]; + NSString* title = [command argumentAtIndex:1]; + NSArray* buttons = [command argumentAtIndex:2]; + NSString* defaultText = [command argumentAtIndex:3]; - [self showDialogWithMessage:message title:title buttons:buttons callbackId:callbackId]; + [self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT]; } /** - Callback invoked when an alert dialog's buttons are clicked. - Passes the index + label back to JS - */ + * Callback invoked when an alert dialog's buttons are clicked. + */ - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { CDVAlertView* cdvAlertView = (CDVAlertView*)alertView; - CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:++buttonIndex]; - + CDVPluginResult* result; + + // Determine what gets returned to JS based on the alert view type. + if (alertView.alertViewStyle == UIAlertViewStyleDefault) { + // For alert and confirm, return button index as int back to JS. + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:buttonIndex + 1]; + } else { + // For prompt, return button index and input text back to JS. + NSString* value0 = [[alertView textFieldAtIndex:0] text]; + NSDictionary* info = @{ + @"buttonIndex":@(buttonIndex + 1), + @"input1":(value0 ? value0 : [NSNull null]) + }; + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info]; + } [self.commandDelegate sendPluginResult:result callbackId:cdvAlertView.callbackId]; } |