views:

1225

answers:

6

Is it possible to show an alertview with a textbox inside like the AppStore app. It asks for password in such a dialog. I've seen atleast a couple of other third party apps using it. Is it a private API?

+7  A: 

Yes, it's undocumented. To add a text field to UIAlertView, use addTextFieldWithValue: label: method. You call with the default text as the first argument and the text that displays in an empty field as the second. Having done that, you can access the field via using textFieldAtIndex:n - see below.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Who are you?"     
                       message:@"Give your full name" 
                       delegate:self  cancelButtonTitle:@"Cancel"   
                       otherButtonTitles:@"OK", nil]; 
[alert addTextFieldWithValue:@""label:@"Name"]; 

// Customise name field 
UITextField* name = [alert textFieldAtIndex:0]; 
name.clearButtonMode = UITextFieldViewModeWhileEditing; 
name.keyboardType = UIKeyboardTypeAlphabet; 
name.keyboardAppearance = UIKeyboardAppearanceAlert; 
[alert show];

The next snippet shows how to retrieve the value in the name field:

NSLog("Name is %@", [[modalView textFieldAtIndex:0] text]);
Jane Sales
As other people already mentioned, this will get you kicked from the app store nowadays.
zoul
Agreed - this was written a long time ago. Should I delete the answer.
Jane Sales
+3  A: 

Jeff Lamarche posted some sample code on his blog to do just this. The formatting looked a bit wonky when I tried it but it's probably a good starting point.

Stephen Darlington
A: 

Don't use. They'll reject it. My app has just been rejected.

Aleks N.
A: 

I confirm what eisernWolf said, Apple reject my app for using this method. better add the textbox as a sub view

Tamer
A: 

Private APIs are in the top 3, as Steve Jobs said during 2010 keynote speech.

Bovine
A: 

How would Apple ever know if you used the illegal addTextFieldWithValue way... or used the legal "add my own sub view" way?

(Since Apple never sees anyone's source code.)

Sally
Static code analysis.
taspeotis