Skip to content

Instantly share code, notes, and snippets.

@mywizz
Created December 26, 2011 07:20
Show Gist options
  • Save mywizz/1520690 to your computer and use it in GitHub Desktop.
Save mywizz/1520690 to your computer and use it in GitHub Desktop.
Determines NSString has URL in it, returns extracted URLs with duplicates removed.
#import <Foundation/Foundation.h>
@interface NSString (URLDetect)
-(BOOL)hasURLs;
-(NSArray *)componentsByDetectedURLs;
@end
#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