5

Usually, when I want to load an HTML string into a webview using javascript, I use something like this...

NSString *htmlString = @"HTML String";
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('elementid').innerHTML = \"%@\";", htmlString]];

While this appears to work well for small strings, it has no effect when the string it relatively large. Apparently, there is a length limit.

So, my question here is, if anyone knows of a way to load a large string into a UIWebView without having to reload the webview?

UPDATE: To be a little clear, in my case here, the webview is already loaded, I just want to replace it's content without having to reload it, mainly because reloading the webview is not fast enough for my use.

Sam J.
  • 685
  • 1
  • 8
  • 22

2 Answers2

5

I'm able to pass extremely long strings with stringByEvaluatingJavaScriptFromString, so I doubt that is the problem. (Note I'm writing for osx, not ios which might make a difference)

It might be that your are not escaping the html correctly so it's being passed as invalid javascript, which could cause nothing to happen. I'm doing the following to a string before passing it to javascript:

content = [content stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
content = [content stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
content = [content stringByReplacingOccurrencesOfString:@"\r" withString:@""];
NSString *js = [NSString stringWithFormat:@"set_content(\"%@\")", content];
[ web_view stringByEvaluatingJavaScriptFromString: js ];

I don't know if all of that is necessary or either it could be written more succinctly (I'm very new to objective c), but it seems to work fine for me.

SColvin
  • 11,584
  • 6
  • 57
  • 71
  • This is a good answer. I'd recommend adding the following to also escape single quotes: [content stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"]; – Willster Apr 02 '14 at 12:17
  • Getting around to trying this again after a long time, escaping the html this way seems to make it work, at least on iOS 8. – Sam J. May 22 '15 at 05:35
1

If the HTML is in a file, you could do this:

NSString *path = [[NSBundle mainBundle] pathForResource: @"MyHTML" ofType: @"html"];
NSData *fileData = [NSData dataWithContentsOfFile: path];
[webView loadData: fileData MIMEType: @"text/html" textEncodingName: @"UTF-8" baseURL: [NSURL fileURLWithPath: path]];  

If you literally want to load an HTML string, try this:

NSString *embedHTML = @"<html><head></head><body><p>Hello World</p></body></html>";
[webView loadHTMLString: embedHTML baseURL: nil]; 
Axeva
  • 4,697
  • 6
  • 40
  • 64
  • Thanks, but, both of these approaches require loading/reloading the webview, which is not fast enough for my use. – Sam J. May 10 '13 at 05:24
  • If that's the case I think your only option is to feed the HTML to the WebView using multiple calls to stringByEvaluatingJavaScriptFromString . Feed the content in drips and handle it programmatically in the JavaScript. – Axeva May 13 '13 at 13:59