【发布时间】:2011-05-12 04:45:27
【问题描述】:
我试图从一个简单的 NSString 语句中获取一个 URL。为此,我在以下代码中使用 NSDataDetector:
NSString *string = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it and a number 097843.";
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypeLink) {
NSString *matchingString = [match description];
NSLog(@"found URL: %@", matchingString);
}
}
结果是它找到了 URL 和数字。该号码被检测为电话号码:
found URL: http://abc.com/efg.php?EFAei687e3EsA
found URL: tel:097843
这是一个错误吗?谁能告诉我如何在没有电话号码的情况下获取 URL?
【问题讨论】:
-
好的,知道了!为了防止获取电话号码,我正在检查前七个字母,例如: NSString *httpString = [aString substringWithRange:NSMakeRange(0,7)]; if ([httpString isEqualToString:@"http://"]) { // 我的编码 }
-
这真的很奇怪,因为还有另一个名为
NSTextCheckingTypePhoneNumber的 NSTextCheckingType 专门用于检查电话号码。
标签: cocoa-touch