how to access the secureTextEntry for further use? - iphone

Hi I'm using the below code which includes the alertView containing UserID and Password field.
these UserID and Password are text field, i made password field as secure, but when i later want to access the data entered in password field, its showing no value in password field..
My code is
self.loginPassword = [[ UIAlertView alloc] initWithTitle:#"Please Login" message:#"Enter Valid UserID and Password\n\n\n\n" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
//[self.loginPassword setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
self.userIDText = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)];
self.userIDText.text=#"";
[self.userIDText setBackgroundColor:[UIColor whiteColor]];
[self.userIDText setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.userIDText setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.userIDText setTextAlignment:UITextAlignmentLeft];
self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(12.0,115.0, 260.0, 25.0)];
self.passwordField.text=#"";
[self.passwordField setBackgroundColor:[UIColor whiteColor]];
[self.passwordField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.passwordField setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.passwordField setTextAlignment:UITextAlignmentLeft];
[self.loginPassword addSubview:self.userIDText];
[self.loginPassword addSubview:self.passwordField];
self.passwordField.secureTextEntry = YES;
[self.loginPassword setTag:2];
[self.loginPassword show];
[self.loginPassword release];
please suggest me some solution..

Create and In-built UIAlertView
UIAlertView *alert = [[UIAlertView alloc] init];
// Define Alertview Type to Login&Password
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
// Set Delegate to access Input Value
[alert setDelegate:self];
// show it
[alert show];
set Delegate in you interface (.h file) UIAlertViewDelegate
Implement This delegate method in your implementation (.m file)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Access your Username & Password from alertview and store them for further use.
NSString *username = [[alertView textFieldAtIndex:0] text];
NSString *password = [[alertView textFieldAtIndex:1] text];
NSLog(#"Username : %# , Password : %#",username, password);
}

This may help you.Just take a look at the code.
https://github.com/josecastillo/EGOTextFieldAlertView

Related

How to generate multi-line rather than single line with UIAlertView?

This is my code
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"Continue \n Shopping", nil];
alert.tag = 20;
[alert show];
I get output like this:
I need like this:
I have manual solution for you. But I think its not a good solution:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"", nil];
UILabel *buttonTitle = [[UILabel alloc] initWithFrame:CGRectMake(148, 102, 125, 40)];
buttonTitle.text = #"Continue Shopping";
buttonTitle.font = [UIFont boldSystemFontOfSize:15];
buttonTitle.textColor = [UIColor whiteColor];
buttonTitle.textAlignment = UITextAlignmentCenter;
buttonTitle.backgroundColor = [UIColor clearColor];
buttonTitle.numberOfLines = 2;
[alert addSubview:buttonTitle];
alert.tag = 20;
[buttonTitle release];
[alert show];
You can use it anyway....
UIAlertView doesn't support multi-line buttons. One option would be to just use "Continue" instead of "Continue Shopping" as the button title, otherwise you'd have to use a custom alert view component, e.g. CODialog (you might have to customize it a bit to allow multi-line buttons, but it should be easy).
I think you can try to set numbersOfLine property to label on button. But i don`t know will apple aprove that.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"Continue \n Shopping", nil];
NSArray *subviewsArray = [alert subviews];
for (UIView *subview in subviewsArray) {
if ([subview isKindOfClass:[UIButton class]]) {
NSArray *btnSubviews = [subview subviews];
for (UIView *btnSubview in btnSubviews) {
if ([btnSubview isKindOfClass:[UILabel class]]) {
UILabel *title = (UILabel *)btnSubview;
title.numberOfLines = 2;
}
}
}
}
alert.tag = 20;
[alert show];
[alert release];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"Continue \n Shopping", nil];
alert.tag = 20;
[[[alert buttons] objectAtIndex:1] setLineBreakMode:UILineBreakModeWordWrap];
[alert show];
This code does exactly what you need.
Note, however that
- [UIAlertView buttons] is undocumented
- [UIButton setLineBreakMode] is deprecated. You can however, replace it with
[button.titleLabel setLineBreakMode] which is essentially the same.

AlertView design (to format it with textfields)

I'm using an AlertView to make a login/password screen. I have some problem to format the view correctly. I want to have a AlertView as high as that want (check printscreen if I'm not clear ) but I don't know how!
Here's the code I've made :
// Ask for Username and password.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Login et mot de passe" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Sauvegarder", nil];
// Adds a username Field
textfieldName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
textfieldName.placeholder = #"Login";
[textfieldName setBackgroundColor:[UIColor whiteColor]];
textfieldName.enablesReturnKeyAutomatically = YES;
[textfieldName setReturnKeyType:UIReturnKeyDone];
[textfieldName setDelegate:self];
[alertView addSubview:textfieldName];
// Adds a username
textfieldPassword = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 25.0)];
textfieldPassword.placeholder = #"Password";
[textfieldPassword setBackgroundColor:[UIColor whiteColor]];
textfieldPassword.enablesReturnKeyAutomatically = YES;
[textfieldPassword setReturnKeyType:UIReturnKeyDone];
[textfieldPassword setDelegate:self];
[alertView addSubview:textfieldPassword];
// Show alert on screen.
[alertView show];
[alertView release];
Alert view will adjust its contents (title and message) Try to add lines to the message:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Login et mot de passe"
message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Annuler"
otherButtonTitles:#"Sauvegarder", nil];

Alert View show me warning

When I use alertView using below code, it shows me the warning
warning: Semantic Issue: Method '-addTextFieldWithValue:label:' not found (return type defaults to 'id')
Here is the code:
UIAlertView *alSave=[[UIAlertView alloc]initWithTitle:#"Save as" message:#"Title the note and click Save" delegate:self cancelButtonTitle:#"save" otherButtonTitles:#"cancel", nil];
NSArray *arr=[noteObj.noteTitle componentsSeparatedByString:#" - "];
app.longClickId = [noteObj.noteId integerValue];
[alSave addTextFieldWithValue:[NSString stringWithFormat:#"%#",[arr objectAtIndex:0]] label:#"Note Name"];
// show me warning at this place
textField = [alSave textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.keyboardAppearance = UIKeyboardAppearanceAlert;
textField.autocorrectionType = UITextAutocorrectionTypeNo; // correction automatically
[alSave show];
if (app.NotePopOver!= nil) {
[app.NotePopOver dismissPopoverAnimated:YES];
}
[alSave release];
If you use a private method (of which addTextFieldWithValue: is one), then Apple will most likely reject your app. You can achieve the same result with the following snippet, courtesy of this answer which credits a no longer working link:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Your title here!" message:#"this gets covered" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[myAlertView setTransform:myTransform];
[myAlertView show];
[myAlertView release];
That method is undocumented. You will have to create your own text field and then add it to the alert view.

How to prompt user for text input in Alert view

I am writing a section of code where it would be best if I could use a pop up box something like UIAlertView and prompt the user to enter text like a password.
Any pointers on an elegant way of doing this?
Things are much simpler in iOS 5, just set the alertViewStyle property to the appropriate style (UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, or UIAlertViewStyleLoginAndPasswordInput). Example:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Password" message:#"Enter your password:" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *passwordTextField = [alertView textFieldAtIndex:0];
[alertView show];
> Simple You can apply like this
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Filename" message:#"Enter the file name:" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *passwordTextField = [alertView textFieldAtIndex:0];
[alertView show]
The best way that I've found to do this is to follow this tutorial: http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html
The code used to achieve this is (taken directly from that great tutorial):
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"Server Password" message:#"\n\n\n"
delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel",nil) otherButtonTitles:NSLocalizedString(#"OK",nil), nil];
UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)];
passwordLabel.font = [UIFont systemFontOfSize:16];
passwordLabel.textColor = [UIColor whiteColor];
passwordLabel.backgroundColor = [UIColor clearColor];
passwordLabel.shadowColor = [UIColor blackColor];
passwordLabel.shadowOffset = CGSizeMake(0,-1);
passwordLabel.textAlignment = UITextAlignmentCenter;
passwordLabel.text = #"Account Name";
[passwordAlert addSubview:passwordLabel];
UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"passwordfield" ofType:#"png"]]];
passwordImage.frame = CGRectMake(11,79,262,31);
[passwordAlert addSubview:passwordImage];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.font = [UIFont systemFontOfSize:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert setTransform:CGAffineTransformMakeTranslation(0,109)];
[passwordAlert show];
[passwordAlert release];
[passwordField release];
[passwordImage release];
[passwordLabel release];
If my app was not to be released for yet a months or two, then I would login to http://developer.apple.com, look at the iOS 5 beta area, and see if UIAlertView might have something in store for us.
I think it would be helpful to know that UIAlertView is not modal so the alert will not block.
I ran into this problem where I wanted to prompt the user for input then continue and then use that input in the code after. But instead the code after the [alert show] would run first until you reached the end of the run loop then the alert would display.
Optimized code:
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"Password"
message:#"Please enter the password:\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel",nil)
otherButtonTitles:NSLocalizedString(#"OK",nil), nil];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.borderStyle = UITextBorderStyleRoundedRect;
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert show];
[passwordAlert release];
[passwordField release];

Showing a alertview with a textbox in iPhone

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?
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]);
This is a really old questions with really old answers.
This is a sample of how I get a UITextfield into a UIAlertView since ios 5:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"New List Name" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
message.alertViewStyle = UIAlertViewStylePlainTextInput;
self.alertTextField = [message textFieldAtIndex:0];
self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
message.delegate = self;
[message show];
[self.alertTextField becomeFirstResponder];
where alertTextField was set up like this:
#property (nonatomic, strong) UITextField *alertTextField;
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.