Nit*_*ish 7 iphone ios nsdatadetector
我有一个来自服务器的字符串,想检查它是否包含电话号码,邮件地址和电子邮件等表达式.我在电话号码和邮件地址方面取得了成功,但没有收到电子邮件.我是NSDataDetector
为了这个目的而使用的.例如
NSString *string = sourceNode.label; //coming from server
//Phone number
NSDataDetector *phoneDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:nil];
NSArray *phoneMatches = [phoneDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in phoneMatches) {
if ([match resultType] == NSTextCheckingTypePhoneNumber) {
NSString *matchingStringPhone = [match description];
NSLog(@"found URL: %@", matchingStringPhone);
}
}
Run Code Online (Sandbox Code Playgroud)
但是如何为电子邮件做同样的事情呢?
mkt*_*kto 27
if(result.resultType == NSTextCheckingTypeLink)
{
if([result.URL.absoluteString rangeOfString:@"mailto:"].location != NSNotFound)
{
// email link
}
else
{
// url
}
}
Run Code Online (Sandbox Code Playgroud)
电子邮件地址属于NSTextCheckingTypeLink.只需在找到的URL中查找"mailto:",您就会知道它是一封电子邮件或URL.
尝试以下代码,看看它是否适合您:
NSString * mail = so@so.com
NSDataDetector * dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSTextCheckingResult * firstMatch = [dataDetector firstMatchInString:mail options:0 range:NSMakeRange(0, [mail length])];
BOOL result = [firstMatch.URL isKindOfClass:[NSURL class]] && [firstMatch.URL.scheme isEqualToString:@"mailto"];
Run Code Online (Sandbox Code Playgroud)
在苹果文档中,似乎识别的类型不包括电子邮件:http : //developer.apple.com/library/IOs/#documentation/AppKit/Reference/NSTextCheckingResult_Class/Reference/Reference.html#//apple_ref/c/tdef /NSTextCheckingType
所以我建议你使用正则表达式。它会像:
NSString* pattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]+";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
if ([predicate evaluateWithObject:@"johndoe@example.com"] == YES) {
// Okay
} else {
// Not found
}
Run Code Online (Sandbox Code Playgroud)
编辑 :
因为@dunforget 得到了最好的解决方案,尽管我是公认的,请阅读他的回答。
这是一个干净的 Swift 版本。
extension String {
func isValidEmail() -> Bool {
guard !self.lowercaseString.hasPrefix("mailto:") else { return false }
guard let emailDetector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue) else { return false }
let matches = emailDetector.matchesInString(self, options: NSMatchingOptions.Anchored, range: NSRange(location: 0, length: self.characters.count))
guard matches.count == 1 else { return false }
return matches[0].URL?.absoluteString == "mailto:\(self)"
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 3.0 版本:
extension String {
func isValidEmail() -> Bool {
guard !self.lowercased().hasPrefix("mailto:") else { return false }
guard let emailDetector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) else { return false }
let matches = emailDetector.matches(in: self, options: NSRegularExpression.MatchingOptions.anchored, range: NSRange(location: 0, length: self.characters.count))
guard matches.count == 1 else { return false }
return matches[0].url?.absoluteString == "mailto:\(self)"
}
}
Run Code Online (Sandbox Code Playgroud)
Objective-C:
@implementation NSString (EmailValidator)
- (BOOL)isValidEmail {
if ([self.lowercaseString hasPrefix:@"mailto:"]) { return NO; }
NSDataDetector* dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
if (dataDetector == nil) { return NO; }
NSArray* matches = [dataDetector matchesInString:self options:NSMatchingAnchored range:NSMakeRange(0, [self length])];
if (matches.count != 1) { return NO; }
NSTextCheckingResult* match = [matches firstObject];
return match.resultType == NSTextCheckingTypeLink && [match.URL.absoluteString isEqualToString:[NSString stringWithFormat:@"mailto:%@", self]];
}
@end
Run Code Online (Sandbox Code Playgroud)