Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C - Finding a URL within a string

Given a large string, what is the best way to create an array of all valid urls which are contained within the string?

like image 839
Mick Walker Avatar asked May 14 '11 00:05

Mick Walker


1 Answers

No need to use RegexKitLite for this, since iOS 4 Apple provide NSDataDetector (a subclass of NSRegularExpression).

You can use it simply like this (source is your string) :

NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:source options:0 range:NSMakeRange(0, [source length])];
like image 184
gcamp Avatar answered Sep 30 '22 03:09

gcamp