diff --git a/src/android/PinDialog.java b/src/android/PinDialog.java index 6be1d6e..cbd27a7 100644 --- a/src/android/PinDialog.java +++ b/src/android/PinDialog.java @@ -9,17 +9,15 @@ import org.json.JSONObject; import android.app.AlertDialog; -import android.app.ProgressDialog; import android.content.DialogInterface; import android.text.InputType; import android.text.method.PasswordTransformationMethod; +import android.view.WindowManager; import android.widget.EditText; public class PinDialog extends CordovaPlugin { - public ProgressDialog spinnerDialog = null; - public PinDialog() { } @@ -108,9 +106,11 @@ public void onCancel(DialogInterface dialog){ } }); - dlg.create(); - dlg.show(); - + AlertDialog instance = dlg.create(); + instance.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); + instance.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + instance.show(); + promptInput.requestFocus(); }; }; this.cordova.getActivity().runOnUiThread(runnable); diff --git a/src/ios/CDVPinDialog.h b/src/ios/CDVPinDialog.h index 415195d..8ca077d 100644 --- a/src/ios/CDVPinDialog.h +++ b/src/ios/CDVPinDialog.h @@ -10,7 +10,7 @@ #import -@interface CDVPinDialog : CDVPlugin {} +@interface CDVPinDialog : CDVPlugin {} @property (nonatomic, copy) NSString* callbackId; - (void)prompt:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/CDVPinDialog.m b/src/ios/CDVPinDialog.m index bd33a20..f817fac 100644 --- a/src/ios/CDVPinDialog.m +++ b/src/ios/CDVPinDialog.m @@ -15,47 +15,34 @@ - (void)prompt:(CDVInvokedUrlCommand*)command NSString* message = [command argumentAtIndex:0]; NSString* title = [command argumentAtIndex:1]; NSArray* buttons = [command argumentAtIndex:2]; - - UIAlertView* alertView = [[UIAlertView alloc] - initWithTitle:title - message:message - delegate:self - cancelButtonTitle:nil - otherButtonTitles:nil]; - - //alertView.callbackId = callbackId; - - int count = [buttons count]; - - for (int n = 0; n < count; n++) { - [alertView addButtonWithTitle:[buttons objectAtIndex:n]]; - } - - alertView.alertViewStyle = UIAlertViewStyleSecureTextInput; - UITextField* textField = [alertView textFieldAtIndex:0]; - - [alertView show]; - [textField resignFirstResponder]; - [textField setKeyboardType:UIKeyboardTypeNumberPad]; - [textField becomeFirstResponder]; - + dispatch_async(dispatch_get_main_queue(), ^{ + UIAlertController* alert = [UIAlertController alertControllerWithTitle:title + message:message preferredStyle:UIAlertControllerStyleAlert]; + [alert addTextFieldWithConfigurationHandler:^(UITextField* tf) { + tf.secureTextEntry = YES; + tf.keyboardType = UIKeyboardTypeNumberPad; + }]; + __weak UIAlertController* weakAlert = alert; // avoid action-handler retain cycle + for (NSUInteger n = 0; n < buttons.count; n++) { + NSInteger oneBased = (NSInteger)n + 1; + UIAlertActionStyle style = (n == buttons.count - 1) + ? UIAlertActionStyleCancel : UIAlertActionStyleDefault; + [alert addAction:[UIAlertAction actionWithTitle:buttons[n] + style:style handler:^(UIAlertAction* a) { + NSString* value0 = weakAlert.textFields.firstObject.text; + NSDictionary* info = @{ @"buttonIndex": @(oneBased), + @"input1": (value0 ?: [NSNull null]) }; + CDVPluginResult* r = [CDVPluginResult + resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info]; + [self.commandDelegate sendPluginResult:r callbackId:self.callbackId]; + }]]; + } + UIViewController* top = self.viewController; + while (top.presentedViewController) top = top.presentedViewController; + [top presentViewController:alert animated:YES completion:nil]; + }); } - - -- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex -{ - CDVPluginResult* result; - 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:self.callbackId]; -} - @end