Open HTML with link using NSAttributedString

0

I get a similar HTML from the server:

"<br>Dummy text<br>Click <a href="www.google.com">Here</a>"

No Click Here should be possible to click and open in Safari. I tried to implement the following code: (being textMsg a TextView).

 NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[[self.myServer textHTML] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
[self.textMsg setEditable:NO];
[self.textMsg setScrollEnabled:NO];
[self.textMsg setDataDetectorTypes:UIDataDetectorTypeLink];
self.textMsg.attributedText = attrStr ;

I also tried other similar ways, but none worked.

In android there is a similar function, Html.fromHtml ().

Receiving the link in this way, is there a way to get it redirected to the browser?

    
asked by anonymous 22.09.2014 / 15:42

1 answer

1

The url does not open automatically in the browser because the protocol is not indicated. If you include the protocol, the link will open in Safari:

@"<br>Dummy text<br>Click <a href=\"http://www.google.com\">Here</a>"

If you want more control over the action when clicking the link, implement the - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange of delegate method of UITextView

    
22.09.2014 / 17:57