Created
December 26, 2011 07:20
-
-
Save mywizz/1520690 to your computer and use it in GitHub Desktop.
Determines NSString has URL in it, returns extracted URLs with duplicates removed.
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
@interface NSString (URLDetect) | |
-(BOOL)hasURLs; | |
-(NSArray *)componentsByDetectedURLs; | |
@end |
This file contains hidden or 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
#import "NSString+URLDetect.h" | |
@implementation NSString (URLDetect) | |
-(BOOL)hasURLs | |
{ | |
return [[self componentsByDetectedURLs] count] > 0; | |
} | |
-(NSArray *)componentsByDetectedURLs | |
{ | |
NSMutableArray *urls = [NSMutableArray new]; | |
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil]; | |
NSArray *matches = [detector matchesInString:self options:0 range:NSMakeRange(0, [self length])]; | |
for (NSTextCheckingResult *match in matches) | |
{ | |
if ([match resultType] == NSTextCheckingTypeLink) | |
{ | |
NSString *url = [[match URL] absoluteString]; | |
if ([urls indexOfObject:url] == NSNotFound) | |
{ | |
[urls addObject:url]; | |
} | |
} | |
} | |
return urls; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment