NSError *error = nil;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
NSString *stringValue = @"Two links: useyourloaf.com and apple.com";
NSURL *url = nil;
NSTextCheckingResult *result = [detector firstMatchInString:stringValue
options:0
range:NSMakeRange(0, stringValue.length)];
if (result.resultType == NSTextCheckingTypeLink)
{
url = result.URL;
}
NSLog(@"matched: %@", url);
// matched: http://useyourloaf.com
//--------------------------------------------------------------------
NSString *stringValue = @"Two links: useyourloaf.com and apple.com";
[detector enumerateMatchesInString:stringValue
options:0
range:NSMakeRange(0, stringValue.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
if (result.resultType == NSTextCheckingTypeLink)
{
NSLog(@"matched: %@",result.URL);
}
}];
// matched: http://useyourloaf.com
// matched: http://apple.com
//====================================================================
NSString *string = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";
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) {
NSURL *url = [match URL];
NSLog(@"found URL: %@", url);
}
}
//====================================================================
No comments:
Post a Comment