Skip to content

Instantly share code, notes, and snippets.

@xr1337
Created April 5, 2013 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xr1337/5318147 to your computer and use it in GitHub Desktop.
Save xr1337/5318147 to your computer and use it in GitHub Desktop.
Taken from https://gist.github.com/iangmaia/3368412 and added an ignore to certain URL schemes such as mailto
- (NSArray *) extractLinksFromString:(NSString*)txt ignoreSchemes:(NSArray*)ignoreSchemes{
NSError *error = NULL;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
if (error) return nil;
NSArray *linkMatches = [detector matchesInString:txt options:0 range:NSMakeRange(0, txt.length)];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[linkMatches count]];
for (NSTextCheckingResult *match in linkMatches) {
if ([match resultType] == NSTextCheckingTypeLink) {
if ([ignoreSchemes containsObject:match.URL.scheme]) {
continue;
}
[result addObject:[[match URL] absoluteString]];
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment