Saturday, 14 July 2012

Find URL from NSString

If you want to fetch a URL from a String:



NSString *StringContainingURL = @"You can click http://objectivecwithsuraj.blogspot.in/";
NSDataDetector *Detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [Detector matchesInString: StringContainingURL  options:0 range:NSMakeRange(0, [StringContainingURL  length])];
        for (NSTextCheckingResult *match in matches)
        {
            if ([match resultType] == NSTextCheckingTypeLink)
            {
                NSURL *url = [match URL];
                NSLog(@"URL : %@",url); 
            }
        }
   

Output of the log is:

No comments:

Post a Comment