NSDataDetector
necessarily detects telephone numbers as links because on the phone, you can tap them as if they were a link in order to initiate a phone call (or tap and hold to initiate a text message, etc). I believe that the current locale (ie, NSLocale
) is what determines whether a string of numbers looks like a phone number or not. For example, in the United States, it would take at least seven digits to be recognized as a phone number, since numbers in the US are of the general form: d{3}-d{4}
.
As for recognizing a telephone link versus another link, it's not a good idea to check for http://
at the beginning of the URL. A simple example suffices: what if it's an https://
link? Then your code breaks.
The better way to check this would be like this:
NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
NSURL *url = [match URL];
if ([[url scheme] isEqual:@"tel"]) {
NSLog(@"found telephone url: %@", url);
} else {
NSLog(@"found regular url: %@", url);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…