Created
April 5, 2013 10:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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