20

How to load a string like below in UIWebview

"\n
\nEgestas rhoncus auctor ac. Risus parturient, mid ultrices nisi.\U00a0
\nAugue ac elementum duis aliquet dolor elementum cum?\U00a0
\nTristique, augue sit lorem adipiscing dis!\U00a0
\nNunc nunc ultricies pellentesque dis dictumst enim
\n
\n"

I am trying to load this same content in 5 webviews..But it is crashing for me..

1
  • 1
    if u load this string and want to keep the same number of lines u should consider replacing the \n with <br/>
    – alinoz
    Nov 12, 2012 at 12:42

4 Answers 4

73

You should use:

[webView loadHTMLString:string baseURL:nil];

But note, that you won't preserve line breaks. To preserve line breaks you'll need to replace \n with <\br> (or any valid line breaking html), so this turns into:

[webView loadHTMLString:[string stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"] baseURL:nil];
1
14

This is pretty simple As Below,

 [self.webView loadHTMLString:htmlString baseURL:nil];

You can specify the baseURL to get relative paths working. Is this what you were looking for ? Or more general explanations about UIWebView ?

8

Here is the Code

 NSString* htmlString= @"hereyourSTring";
 [webView loadHTMLString:htmlString baseURL:nil];

this would load the String in WEBview.

3
  • But it is crashing for me :(
    – Dev
    Nov 12, 2012 at 12:53
  • @Dev can you crash log here..? Nov 12, 2012 at 13:11
  • @Dev i am just asking, put crash log here. if you see your console there would be crash log related to the crash Nov 14, 2012 at 4:32
4

In Swift 3

let webView = UIWebView()
webView.loadHTMLString("htmlString", baseURL: nil)

But note, that you won't preserve line breaks. To preserve line breaks you'll need to replace \n with <\br> as done by @MANIAK_dobrii in above answer

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.