Xcode loadHTMLString baseURL:nil在iOS5中不工作

Xcode loadHTMLString baseURL:nil在iOS5中不工作,xcode,uiwebview,ios5,Xcode,Uiwebview,Ios5,我对UIWebView有问题。我想在UIWebView中呈现我自己的html代码 如果我使用下面的代码,它工作得很好 NSBundle *thisBundle = [NSBundle mainBundle]; NSString *path = [thisBundle pathForResource:@"foo" ofType:@"html"]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [self.webView

我对UIWebView有问题。我想在UIWebView中呈现我自己的html代码

如果我使用下面的代码,它工作得很好

    NSBundle *thisBundle = [NSBundle mainBundle];
    NSString *path = [thisBundle pathForResource:@"foo" ofType:@"html"];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    [self.webView loadHTMLString:@"foo.html" baseURL:baseURL];
我想用下面的代码呈现html。但它不起作用。html包含与NSString*one完全相同的内容。有什么提示吗?提前感谢。=)

NSString*one=@;
[self.webView loadHTMLString:one baseURL:nil];

我使用的代码非常有效:

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

确保image.jpg包含在您的项目中。

创建JS文件demo.JS并添加代码

var hello=function(str){
返回str;
};

添加UIWebView=>以将JavaScript嵌入虚拟html文件并将其加载到UIWebView中

[self.webView加载htmlstring:@“”
baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]resourcePath]]

下面是进行JS调用的代码

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

现在,有一个重要的安全提示可以让它实际工作:UIWebView必须实际加载到一个视图中。它不必是可见的,但为了让WebKit引擎执行JS,它必须位于视图上。

Hi,我不想读取本地html文件。我想这样直接使用。NSString*one=@@月亮:是的,对不起,用你的HTML替换htmlContent。这就是我写这段代码时所做的。我编辑我的答案。
NString *str=@"hi JavaScript";
NSString *function = [[NSString alloc] initWithFormat: @"hello(%@)", str];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:function];