iphone - 使用 NSDataDetector 检查电子邮件

标签 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);
    }
}  

但是如何对电子邮件做同样的事情呢?

最佳答案

if (result.resultType == NSTextCheckingTypeLink)
{
    if ([result.URL.scheme.locaseString isEqualToString:@"mailto"])
    {
        // email link
    }
    else
    {
        // url
    }
}

电子邮件地址属于 NSTextCheckingTypeLink。只需在找到的 URL 中查找“mailto:”,您就会知道它是电子邮件或 URL。

关于iphone - 使用 NSDataDetector 检查电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8850918/

相关文章:

iphone - iphone sdk中nsdata检测器的内部功能是什么

ios - 通过 NSDataDetector 的 NSTextCheckingTypeTransitInformation 检测航类信息永远不会匹配任何内容

iphone - 我的 UITableViewCell 中没有文字

iphone - 如何在 iPhone 上显示 NSAttributedString?

iphone - 更改 cocos2D 中 CCSprite 的父级

ios - 无法快速符合 objective-c 协议(protocol)

iphone - 解析 - 应用程序终止错误

iphone - 如何在 Xcode 模板中包含静态库依赖项?

ios - UIViewController 的 subview 未显示

ios - NSDataDetector 类型 NSTextCheckingTypeTransitInformation - 它可用于 iOS 吗?