SpotHeroEmailValidator
一个 iOS 库,它将为基本的电子邮件语法验证提供支持,并提供可能的错误提示(例如,[email protected] 将更正为 [email protected])。
截图
使用方法
UITextField 子类
SHEmailValidationTextField 类是一个预先构建的 UITextField 子类,当它失去焦点时(例如,当用户从电子邮件字段切换到密码字段时),将自动验证其输入。如果检测到语法错误,将弹出一个提示框通知用户他们输入的电子邮件地址无效。如果检测到可能出现的错误,将弹出一个提示框,允许用户接受建议或关闭提示框。使用此类的方法是将UITextField实例替换为SHEmailValidationTextField。
自定义
要自定义默认的错误消息,请使用 setDefaultErrorMessage:
方法。要为特定的错误设置特定消息,请使用 setMessage:forErrorCode:
方法。要自定义显示在拼写建议上方文本,请使用 setMessageForSuggestion:
方法。
要自定义弹出窗口的外观和感觉,可以设置所需的 fillColor
、titleColor
和 suggestionColor
属性。
基本语法检查
NSError *error = nil;
[[[SpotHeroEmailValidator] validator] validateSyntaxOfEmailAddress:emailAddress withError:&error];
if (error) {
// An error occurred
switch (error.code) {
case SHBlankAddressError:
// Input was empty
break;
case SHInvalidSyntaxError:
// Syntax completely wrong (probably missing @ or .)
break;
case SHInvalidUsernameError:
// Local portion of the email address is empty or contains invalid characters
break;
case SHInvalidDomainError:
// Domain portion of the email address is empty or contains invalid characters
break;
case SHInvalidTLDError:
// TLD portion of the email address is empty, contains invalid characters, or is under 2 characters long
break;
}
} else {
// Basic email syntax is correct
}
获取拼写更正建议
NSError *error = nil;
NSString *suggestion = [[[SpotHeroEmailValidator] validator] autocorrectSuggestionForEmailAddress:emailAddress withError:&error];
if (error) {
// The syntax check failed, so no suggestions could be generated
} else if (suggestion) {
// A probable typo has been detected and the suggestion variable holds the suggested correction
} else {
// No typo was found, or no suggestions could be generated
}
更新 IANA TLD 列表
要获取最新的 IANA TLD,请运行包含在根目录中的以下脚本:fetch_iana_list.rb
这将更新 SpotHeroEmailValidator/DomainData.plist 下的 plist。脚本需要安装 httparty 和 plist Ruby gems。
ARC
SpotHeroEmailValidator 使用 ARC。如果你的项目不符合 ARC 标准,只需为所有 SpotHeroEmailValidator 源文件设置 -fobjc-arc
标志。
使用此库的应用
此库用于我们自己的SpotHero iOS应用。如果您想在这里看到您的应用程序,请通知我们您正在使用它!
许可
SpotHeroEmailValidator按照Apache 2.0许可证发布。