LoadHTMLString baseURL: nil not working in iOS5

I am having a problem with UIWebView. I want to display my own HTML in a UIWebView.

if i use the code below it works fine.

NSBundle *thisBundle = [NSBundle mainBundle]; NSString *path = [thisBundle pathForResource:@"foo" ofType:@"html"]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [self.webView loadHTMLString:@"foo.html" baseURL:baseURL]; 

I want to display html using the code below. But that does not work. foo.html contains exactly the same content with NSString * one. Any hints? Thanks in advance. =)

  NSString* one = @"<html><head><title></title></head><body><img src=\"image.jpg\"/></body></html>"; [self.webView loadHTMLString:one baseURL:nil] ; 
+6
source share
2 answers

I use this code that works great:

 NSString* htmlContent = @"<html><head><title></title></head><body><img src='image.jpg' /></body></html>"; [self.webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 

Make sure the image.jpg file is included in your project.

+23
source

create js file demo.js and add code

var hello = function(str){ return str; };

add UIWebView => Paste JavaScript in a dummy html file and load it into UIWebView

[self.webView loadHTMLString:@"<script src=\"demo.js\"></script>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

Here is the code to make a JS call

 NString * str=@ "hi JavaScript"; NSString *function = [[NSString alloc] initWithFormat: @"hello(%@)", str]; NSString *result = [webView stringByEvaluatingJavaScriptFromString:function]; 

You now have one important safety tip for this to really work: UIWebView should actually load the view. It should not be visible, but for the WebKit engine to run JS, it must be in the view.

+1
source

Source: https://habr.com/ru/post/900615/


All Articles