Open HTML with link using Nsattributedstring

Asked

Viewed 71 times

0

I get from the server an HTML similar to this:

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

On Click Here it 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 tried it in other similar ways, but none worked..

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

Getting the link this way, there would be a way to get it to redirect to the browser?

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 be opened in Safari:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.