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?
In iOS 16, actions view builder allow two new view, TextField and SecureField . We can have a text field in an alert by using TextField or SecureField as action content. We add text field in actions , not message .
Build and present custom alerts to your users Presenting system alerts on SwiftUI is super simple. Just call an instance method alert and pass a few parameters, and you are done. But the system alerts that come up are too basic with the non-customizable default design.
Here's an "Apple Approved" way of doing it from Tharindu Madushana. I got it from his comment in this page: http://iosdevelopertips.com/undocumented/alert-with-textfields.html
// 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];
And finally to get the text back
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
return; //Cancel
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (@"%@", field.text);
}
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With