我目前正在使用PFUser方法' signUpInBackgroundWithBlock:'注册我的用户,但对我的用户体验的限制意味着我无法在同一个ViewController上签名,因此我尝试在PFUser Parse对象上调用该方法之前验证电子邮件。
另一种方法是将我的用户发送回早期的视图控制器,如果parse在方法调用后给我一个错误(我不想这样做)
我已经找到了这个正则表达式模式,但这是一个相当古老的答案,我知道现在已经出局的有趣域名:
https://www.parse.com/questions/email-validation-rules-for-pfsignupviewcontroller
答案 0 :(得分:1)
“另一种方法是将我的用户发送回早期的视图控制器,如果parse在方法调用后给我一个错误(我不想这样做)”
注意 - 很遗憾,您根本无法构建解析应用,除非您像这样“发回”。不幸的是“就这么简单。”在处理Parse时,每一个这样的“步骤”,你必须能够在你描述的意义上“回归”。
在回答你的问题时,正如你可能已经知道的那样,由于定义电子邮件的性质存在各种问题,而且事实上你只是不知道,因此没有真正好的方法来真正检查字符串是否是电子邮件。实际上,对于任何应用程序,都需要完整的“真正可能”的电子邮件字符串。
它已在许多大批量生产应用程序中使用。
请注意,我认为NSPredicate是在iOS中执行此操作的最自然,最可靠的方法。
-(BOOL)basicLocalEmailCheck
{
if ( self.length > 50 ) return NO;
// note, first if it ends with a dot and one letter - that is no good
// (the regex below from W3C does allow a final single-letter tld)
NSString *rx = @".*\\..$";
NSPredicate *emailTest = [NSPredicate
predicateWithFormat:@"SELF MATCHES %@", rx];
if ( [emailTest evaluateWithObject:self] ) return NO;
// here's the original from the W3C HTML5 spec....
// ^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
// i made a modification,
// you can't have eg "localhost" with no .com,
// and note you have to escape one backslash for the string from the W3C
rx = @"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?){1,5}$";
emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", rx];
return [emailTest evaluateWithObject:self];
}
如果您是初学者并且不熟悉类别,则可以使用它。
以下是一些典型的现实用途......特别是与Parse有关,因为你提到了它。
-(IBAction)joinNow
{
[self.view endEditing:YES];
if ( [self _fieldBlank] )
{ [self woe:@"Please complete all fields."]; return; }
if ( ! [self.email.text basicLocalEmailCheck] )
{ [self woe:@"Please enter a valid email."]; return; }
if ( self.imageHasBeenSet == NO )
{ [self woe:@"Please add a picture."]; return; }
if ( self.password.text.length > 20 ||
self.firstname.text.length > 20 ||
self.surname.text.length > 20 )
{ [self woe:@"20 character limit for names and password."]; return; }
[self ageConfirmThenJoin];
}
-(IBAction)clickLogin:(id)sender
{
[self.view endEditing:YES];
[PFUser logOut];
if ( ! [self.loginEmail.text basicLocalEmailCheck] )
{
[UIAlertView ok:@"Please enter your email in the email field."];
[self begin];
return;
}
[APP huddie];
APP.hud.labelText = @"Logging in ...";
[PFAnalytics trackEvent:@"loginAttempt"];
[PFUser logInWithUsernameInBackground: [self.loginEmail.text lowercaseString]
password: self.loginPassword.text
block:^(PFUser* user, NSError* error)
{
[APP.hud hide:YES];
if (user) // Login successful
{
[PFAnalytics trackEvent:@"loginSuccess"];
[self isLoggedInCheckValid];
return;
}
else
{
// note, with Parse it SEEMS TO BE THE CASE that
// 100, no connection 101, bad user/pass
NSString *msg;
NSString *analyticsMsg = @"otherProblem";
if ( !error)
{
msg = @"Could not connect. Try again later...";
// seems unlikely/impossible this could happen
}
else
{
if ( [error code] == 101 )
{
msg = @"Incorrect email or password. Please try again.";
analyticsMsg = @"passwordWrong";
}
else
{
msg = @"Could not connect. Try again later.";
}
}
[PFAnalytics trackEvent:@"loginFailure"
dimensions:@{ @"reason":analyticsMsg }];
[UIAlertView ok:msg];
[self begin]; // not much else we can do
return;
}
}];
}
答案 1 :(得分:0)