我正在使用此代码重置用户的密码,但是他们遇到了一些困难我找不到用户的正确电子邮件地址,即我在用户UITextField
中输入的内容。它接收任何已注册的电子邮件解析可以。
-(IBAction)ForgotPassword:(id)sender
{
UIAlertView * forgotPassword=[[UIAlertView alloc] initWithTitle:@"Forgot Password" message:@"Please enter your email id" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
forgotPassword.alertViewStyle=UIAlertViewStylePlainTextInput;
[forgotPassword textFieldAtIndex:0].delegate=self;
[forgotPassword show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex ==1){
NSLog(@"ok button clicked in forgot password alert view");
NSString *femailId=[alertView textFieldAtIndex:0].text;
if ([femailId isEqualToString:@""]) {
UIAlertView *display;
display=[[UIAlertView alloc] initWithTitle:@"Email" message:@"Please enter password for resetting password" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[display show];
}else{
[PFUser requestPasswordResetForEmailInBackground:femailId block:^(BOOL succeeded, NSError *error) {
UIAlertView *display;
if(succeeded){
display=[[UIAlertView alloc] initWithTitle:@"Password email" message:@"Please check your email for resetting the password" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
}else{
display=[[UIAlertView alloc] initWithTitle:@"Email" message:@"Email doesn't exists in our database" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
}
[display show];
}];
}
}
}
答案 0 :(得分:0)
替换以下代码
NSString *aStrMSG=[alertView textFieldAtIndex:0].text;
[self.view endEditing:YES];
if([StrValue length] > 0)
{
if(![self validateEmail:StrValue])
{
aStrMSG = @"Please insert valid E-mail address";
if([aStrMSG length] > 0)
{
ShowAlert(aStrMSG);
}
}
else
{
//Success email is perfect
}
}
此处检查电子邮件地址是否正确
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
@"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
@"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
@"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
完美解决方案
这里我添加了解析代码忘记密码:所以用户必须输入他们的电子邮件地址,电子邮件将从解析发送到此 电子邮件ID。
-(IBAction)btnForgotClicked:(id)sender
{
NSLog(@"btnForgotClicked Clicked");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Email Address"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1)
{
NSString *emailaddress = [actionSheet textFieldAtIndex:0].text;
[PFUser requestPasswordResetForEmailInBackground:emailaddress];
HUDSHOW
[PFUser requestPasswordResetForEmailInBackground:emailaddress block:^(BOOL succeeded, NSError *error){
if (!error)
{
HUDHIDE
ShowAlert(@"email sent Succesfully");
}
else
{
NSString *errorString = [error userInfo][@"error"]; // Show the errorString somewhere and let the user try again.
ShowAlert(errorString);
HUDHIDE
}
}];
}
}