I'm building an iOS app and I need to provide a forgot password function. I have a db that stores registered users and their details(userArray). I have searched by email inputed so, how do I do a UIAlert that shows the found password??? below is code I tried but my if condition is wrong.
[self.theuser valueForKey:#"password"] is the retrieved password from the database and its a string.
- (IBAction)ForgotPassword:(id)sender {
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: #"Your Password is:"
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
You need to check that the length of the string is higher than 0, because using this:
if ([self.theuser valueForKey:#"password"])
will always return true if the value is other than nil, meaning if you server code returns that the value of password is "" (two quote marks with no space in between) then the app will recognize that the value is not nil and display an alert with no text in it
So the final code should look something like this:
- (IBAction)ForgotPassword:(id)sender {
NSString *retrievedPassword = [self.theuser valueForKey:#"password"];
if ( retrievedPassword.length > 0) {
NSString *message = [NSString stringWithFormat:#"Your password is: %#", retrievedPassword];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: message
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
}
Change:
#"Your Password is:"
to:
[NSString stringWithFormat:#"Your Password is: %#", [self.theuser valueForKey:#"password"]]
You can also change this:
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
to this:
if ([self.theuser valueForKey:#"password"]) {
If it didn't exist, it would count as "false". Any other value is "true".
Related
I'm trying to build a login page for an app. Something went wrong when testing the login condition.
There are two textfields, username and password. When the Login button is clicked, alert shows when
1) either fields are empty
2) both filled.
The code reflects this below. But the result came out no matter that the textfield is empty or not, it only showed as textfields were filled ( only the else part run). So I assume there was something wrong with the if condition. But when I checked the if condition in other view, it worked.
Can anyone help?
if ([_pLoginIDField.text isEqualToString:#""] || [_pPasswordField.text isEqualToString:#""]) {
UIAlertView *error = [[UIAlertView alloc] initWithTitle:#"Yeah !" message:#"must complete all fields" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error show];
}
else {
UIAlertView *error2 = [[UIAlertView alloc] initWithTitle:#"Oooops !" message:#"hhhhhhhhhh" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error2 show];
}
Try this:
if (_pLoginIDField.text.length > 0 || _pPasswordField.text.length > 0)
NSString *text = [NSString stringWithString: nameUITextField.text];
if(text.length == 0){
}
You may try this,
you can count how many characters are in string, it is indirectly checking to null.
NSString *inputString = [NSString stringWithString: nameUITextField.text];
if(inputString.charachters.count == 0){
// inputString is Blank
// Do your stuff
}else{
//inputString is not blank, Do other Stuff.
}
the above condition satisfy if inputString is blank otherwise it will enter into else block.
Firstly I have this code which saves my video file in the documents directory:
filePath = [NSString stringWithFormat:#"%#/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]];
Secondly I have a UIAlertView that allows the user to enter the name they want for the video they are saving:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"GIVE YOURE VIDEO A NAME" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
And I have this to display what is entered:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput){
NSLog(#"Entered: %#",[[alertView textFieldAtIndex:0] text]);
}
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
Know that I want to do is to save the video file with a name specified by text which was entered in to the UIAlertView. To do this I need to change this line: filePath = [NSString stringWithFormat:#"%#/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]]; which saves the file as the current date and time to the entered text. However, I do not know how I would go about doing that.
Are you asking how to replace the date with the text entered ?
You only need to pass in the string, which you are already doing with the homeDirectory, like so
filePath = [NSString stringWithFormat:#"%#/Documents/%#.mp4", NSHomeDirectory(),[[alertView textFieldAtIndex:0] text]];
I have a calculator that multiplies a value with a certain $ rate. The user can edit the $ rate, and the value is multiplied with the user-entered rate. Here is my code for this:
- (IBAction)edit
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit New Amount"
message:#"Enter new rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex) {
UITextField *field = [alertView textFieldAtIndex:0];
field.placeholder = #"Enter New Rate";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers are allowed in this field."delegate:self cancelButtonTitle:#"OK."otherButtonTitles:nil];
[errorAlert show];
rate.text=#"";
}
else
{
rate.text = field.text;
}
}
else
{
//Cancel
}
}
However, when the app closes and is restarted, the old rate is displayed, not the new user-entered rate. How can I save the new user-entered rate, so that it is retained within the app, even when it is restarted?
If you want the value to be retained after app is closed, then you got to store it somewhere in file system. Easiest and convenient way is to use NSUserDefaults:
//Storing in user defaults
[[NSUserDefaults standardUserDefaults] setObject:field.text forKey:RATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
//Retrieving (possibly in app start or viewdidload)
self.rate.text = [[NSUserDefaults standardUserDefaults] objectForKey:RATE_KEY];
One note on usage of NSUserDefaults is that it doesn't support storing every object type, it supports NSString,NSArray,NSNumber etc and primitive types. Since you used NSStrings in your code I assumed you can save it as string to defaults.
Have you tried creating a new variable to hold the new input as the user enters the new input, and then setting whatever the new input is to that new variable? It sounds like it is restarting from the last variable that was set in the input.
I am building an application which has a login through a mobile SAAS - Parse.
There are multiple error codes that could be returned from a login request. At the moment run an if statement for each error code and display a relevant alert view like this:
if (error == nil) {
// Something went wrong
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:NSLocalizedString(#"LoginStandardError", #"Login error message text - standard error") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
} else if ([error code] == kPFErrorObjectNotFound) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:NSLocalizedString(#"LoginErrorObjectNotFound", #"Login error message text - object not found") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
} else if ([error code] == kPFErrorConnectionFailed) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:NSLocalizedString(#"LoginAlertErrorConnection", #"Login error message text - connection failed") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
} else {
NSLog(#"A Login error occurred: %i",[error code]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:[[error userInfo] objectForKey:#"error"] delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
}
Is there a more efficient way to do the same with case/switching?
The actual error codes are setup like this:
/*! #abstract 100: The connection to the Parse servers failed. */
extern NSInteger const kPFErrorConnectionFailed;
Which makes me think I can setup this in a case statement. Would this be the correct/best way to approach this? Should it be in a separate method like handleErrorAlert: possibly?
How would I code this switch in the example above?
Whether you use a switch statement or a series of if-else if is really just a matter of taste in this case. Yes, the switch statement is slightly more efficient, but in a case like this, it really doesn't matter (it's not like you call this thousands of times per second). Use what you find more readable.
You might want to refactor your alert view code a little though – you're doing the same thing in all cases with only the error message being different, so there's quite a bit of repeated code. You could refactor it like this:
NSString *errorMessage = nil;
if (error == nil) {
errorMessage = NSLocalizedString(#"LoginStandardError", #"Login error message text - standard error");
} else {
switch ([error code]) {
case kPFErrorObjectNotFound:
errorMessage = NSLocalizedString(#"LoginErrorObjectNotFound", #"Login error message text - object not found");
break;
case kPFErrorConnectionFailed:
errorMessage = NSLocalizedString(#"LoginAlertErrorConnection", #"Login error message text - connection failed");
break;
default:
errorMessage = [[error userInfo] objectForKey:#"error"];
}
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title")
message:errorMessage
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
A typedef enum being used on a switch, I think it would be the cleanest way. Something like this:
typedef enum
{
kServerError,
kInternetError,
kUnknowError
} kTypeError;
switch (aTypeError)
{
.
.
.
}
In your specific case, you take care about the message inside the switch... The UIAlertView is a common part. So:
NSString *aTitle = nil;
NSString *aMessage = nil;
switch (aTypeError)
{
case kUnknowError:
{
aTitle = ...;
aMessage = ...;
}
break;
}
UIAlertView *alertView = [UIAlertView alloc] ...
if (!error) {
// Handle error (?).
}
switch ([error code]) {
case kPFErrorObjectNotFound:
// Handle error.
break;
case kPFErrorConnectionFailed:
// Handle error.
break;
default:
// Handle error.
}
This only works if the value returned by -code can be used in a switch test expression. AFAIK, int is supported—I don't know about other types.
I want the user to confirm the password he typed it... so I use two text fields.. But somehow even if both have the same password it seems to think that the 2 strin differ
if (![self.typePTextField.text isEqualToString:self.retypePLabel.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.retypePLabel.text = #"";
return;
}
The alert appears even if I type the same string twice... and only the first text field geets reset to #"" ...
What will fix this?
I'm guessing in the isEqual: method
self.retypePLabel.text
should be
self.retypePTextField.text
try
if (![self.typePTextField.text isEqualToString:self.sometextfield.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.sometextfield.text = #"";
return;
}
if([_txtPassword.text isEqual:_txtconfirmPassword.text])
{
NSLog(#"Password =%# , ConfirmPassword = %# ",_txtPassword.text,_txtconfirmPassword.text);
}
else {
//// code show alert////
}
self.retypePLabel.text? are you sure you ask about this, because I think it should be like self.retypePTextField.text