How to get selected URL when clicked on UITextView?

282 Views Asked by At

My problem is I want to get the URL string from UITextView when I cliked on any URL that contained in it.

1

There are 1 best solutions below

0
On BEST ANSWER
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
textView.dataDetectorTypes = UIDataDetectorTypeAll;
textView.delegate = self; 

// New in iOS 7
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 
    if ([[URL scheme] isEqualToString:@"username"]) { 
        NSString *username = [URL host];  
        // do something with this username 
        // ... 
        return NO; 
    } 
    return YES; // let the system open this URL 
}