Alert with TextFields
Note: Although applications in the App Store have used this approach, Apple has also rejected applications that use this technique as it does access private APIs.
In a recent discussion with the UI designers for an upcoming iPhone application, I was asked if it’s possible to create an Alert that would overlay the splash screen, prompting for a username and password.
My original response (based on searching the documentation) was a regrettable, no. However, after some digging I did come upon an undocumented method inside the UIAlertView: addTextFieldWithValue. What follows is a screenshot of how you might use this feature:

The code for the alert example follows. Notice how you can create a pointer to the UITextFields and modify its attributes, very cool.
In the .h file
1 2 | UITextField *textfieldName; UITextField *textfieldPassword; |
In the .m file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { // Clicked the Submit button if (buttonIndex != [alertView cancelButtonIndex]) { NSLog(@"Name: %@", textfieldName.text); NSLog(@"Name: %@", textfieldPassword.text); } } ... - (void) someMethod { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Login" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit", nil]; [alert addTextFieldWithValue:@"" label:@"User Name"]; [alert addTextFieldWithValue:@"" label:@"Password"]; // Username textfieldName = [alert textFieldAtIndex:0]; textfieldName.keyboardType = UIKeyboardTypeAlphabet; textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert; textfieldName.autocorrectionType = UITextAutocorrectionTypeNo; // Password textfieldPassword = [alert textFieldAtIndex:1]; textfieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing; textfieldPassword.keyboardType = UIKeyboardTypeNumbersAndPunctuation; textfieldPassword.keyboardAppearance = UIKeyboardAppearanceAlert; textfieldPassword.autocorrectionType = UITextAutocorrectionTypeNo; textfieldPassword.secureTextEntry = YES; [alert show]; } |




Would Apple reject any app using this though because it is an undocumented API?
[Reply]
Hi Leon,
I posted an update to the original post as I saw that mJaiku (on the App Store) is using this technique. With that said, Apple may reject an application that uses this approach as it accesses non-published API’s.
John
[Reply]
Hi,
can anybody tell me how can i get back the the action taken by user on alertvew. eg he has pressed yes or no /the data he posted in textfield of alertview
[Reply]
A quick look in the UIAlertView SDK documentation and you’ll find that you can use alertView:didDismissWithButtonIndex: method that specifies the index of the button that was clicked.
[Reply]
How do you get the data from the textfield in the alert view?
[Reply]
Hey Rob,
I updated the example code to show how to access the textfields.
John
[Reply]
Thanks so much for this code! It worked like a charm.
[Reply]
Hi, how about if user inputs a wrong username/password or if the textfields are blank and he presses Submit, how do you prevent the alertview from disappearing?
[Reply]
There is an event ‘UITextFieldTextDidChangeNotification’ that you can use to look at the characters in the textfield. Since you control when the alert is shown and when it is removed from the view, you should be able to get the effect you are after.
[Reply]
Would you be able to provide a quick code on how to implement this? I’m a noob in iPhone programming.
The application I’m developing will initially request for a PIN or password before being allowed to continue. The initialization of the UIAlertView is in the appDelegate and I implemented a alertVew:clickedButtonAtIndex method on the appDelegate wherein I am calling the UIAlertView initWithTitle method again if the textField is 0 which I think my implementation should be improved.
I also noticed that the keyboard is being displayed twice. How do we prevent this from occurring?
Again, thanks for the above code.
Regards,
Wilbert
[Reply]
Hi guys,
I used this code to add a TextField to an UIViewAlert. This is working fine but I have 2 warnings:
warning: ‘UIAlertView’ may not respond to ‘-addTextFieldWithValue:label:’ (Messages without a matching method signature will be assumed to return ‘id’ and accept warning: ‘…’ as arguments.)
warning: ‘UIAlertView’ may not respond to ‘-textFieldAtIndex:’ (Messages without a matching method signature will be assumed to return ‘id’ and accept warning: ‘…’ as arguments.)
Why is that? Is my application will be rejected by Apple if those warnings remain?
Thanks, Regards
Niko
[Reply]
I am getting the same warnings. Any idea? Thanks!
[Reply]
The warnings appear because this is an undocumented use of the API. There are applications in the App store that use this technique, however, you will run the risk that Apple will reject the application for using non-published API’s.
[Reply]
I understand… thank you!
[Reply]
FYI: I was just rejected from the store because of this.
From Apple:
“Upon review of your application, White cannot be posted to the App Store due to the usage of private APIs…The following non-public APIs are included in your application: addTextFieldWithValue:label: textFieldAtIndex:”
[Reply]
John Muchow Reply:
November 12th, 2009 at 7:33 pm
Jason,
Sorry to hear that. Thanks for the heads up.
[Reply]
My application included those APIs and was approved around 3 weeks ago (look for Virtual Sheet Music application).
[Reply]
Thanks Fabrizio,
A testament to the inconsistencies of the Apple review process….ugh.
[Reply]
We got rejected for using that method too…
[Reply]
Guys, I submitted an updated version of my app two days ago and they rejected it because of the use of the private API! Well, that’s very funny because they approved the previous version (which is online!) just a few weeks ago with inside the same, exact code!
Here is the incriminated alert:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @”Send Item Reminder”
message:@”Enter your e-mail”
delegate:delegate
cancelButtonTitle:@”Cancel”
otherButtonTitles:@”GO!”, nil];
[alert addTextFieldWithValue:@"" label:@"your e-mail"];
Any idea how to “modify” it in order to be acceptable? I wish to have an alert that allows users to send their email to my server, so I need to have an email field…
Thanks for any thoughts!
Fab.
[Reply]
Hi Friends,
This way works without any warnings. We are not using Undocumented calls but creating similar UI.
// Ask for Username and password.
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Twitter Details!" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
// Adds a username Field
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; utextfield.placeholder = @"Username";
[utextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:utextfield];
// Adds a password Field
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)]; ptextfield.placeholder = @"Password";
[ptextfield setSecureTextEntry:YES];
[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield];
// Move a little to show up the keyboard
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0);
[alertview setTransform:transform];
// Show alert on screen.
[alertview show];
[alertview release];
//...
// Don't forget to release these after getting their values
[utextfield release];
[ptextfield release];
[Reply]
Hi Tharindu,
Your way to show user name and password in alert view is nice . It does not produce any warning after compilation.
My question is ,
Is there any chance of application getting rejected due to use of above code ?
please reply …
thanks a lot.
S.
[Reply]
Actually this shouldn’t be rejected by Apple any means.
We are not using any Private APIs. Are we ?
[Reply]
Thanks Tharindu, My old code messed up UITextAlertView on the new iOS 4.2.
[Reply]
I have an app rejected because of this technique.
[Reply]
Thanks Tharindu, thank you so much…..
[Reply]