UIWebView loadHTMLString在iOS5中不起作用 [英] UIWebView loadHTMLString not working in iOS5

查看:93
本文介绍了UIWebView loadHTMLString在iOS5中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这与此问题相似,没有回复: loadHTMLString不能与iOS5一起使用?

Perhaps this is similar to this question, which has no responses: loadHTMLString Not Working With iOS5?

我有一个UIWebView,我使用loadHTMLString填充:baseURL:。 HTML小而简单,它引用了一个css样式表和一个javascript脚本,它们通过baseURL:加载,它被设置为应用程序包中的目录。

I have a UIWebView which I populate using loadHTMLString:baseURL:. The HTML is small and simple, and it references a css style sheet and a javascript script, which are loaded via the baseURL:, which is set to a directory inside the app's bundle.

    // load the html
    NSString* filePath = [NSString stringWithFormat: @"%@/html", [[NSBundle mainBundle] resourcePath ] ];
    [_pCurrentWebView loadHTMLString: html baseURL: [NSURL fileURLWithPath: filePath isDirectory: YES ] ];

这在过去一直很有效,但在iOS5中已经破产了。在iOS5中,UIWebView中不显示任何内容。 webview确实提供了所有预期的事件 - 例如shouldLoadRequest,didStartLoad,didFinishLoad等。

This has always worked in the past, but it is broke in iOS5. In iOS5, nothing is displayed in the UIWebView. The webview does source all of the expected events - e.g. shouldLoadRequest, didStartLoad, didFinishLoad, etc.

html有一个脚本标记,如下所示:

The html has a script tag, like this:

<script type="text/javascript" src="./myscript.js" />

如果删除脚本标记,则页面加载并在iOS5中呈现正常。我可以告诉我们加载并应用了与脚本.js文件引用方式相同的css文件。

If I remove the script tag then the page loads and renders fine in iOS5. And I can tell that the css file, which is referenced the same way as the script .js file, is loaded and applied.

如果我保留脚本标记但是make myscript.js文件完全为空它仍然无法加载。

If I keep the script tag but make the myscript.js file completely empty it still fails to load.

对我来说,这似乎是某种跨站点脚本问题 - 因为WebView认为它应该禁止加载脚本(实际上,不允许渲染页面?)

To me, this seems like some sort of cross-site-scripting issue - in that the WebView thinks that it should disallow loading the script (and in fact, disallow rendering of the page??)

不确定从何处开始。想法?

Not sure where to go from here. Ideas?

更新

这种感觉越来越像一个交叉站点脚本问题。如果我删除它的标签,虽然没有脚本。我的所有图像都是从baseURL加载的,我的样式表也是如此。也就是说,我们知道baseURL正在工作。

This is feeling more and more like a cross-site-scripting issue. If I remove the tag it works, albeit sans script. All my images are loaded from the baseURL, as is my stylesheet. That is, we know the baseURL is working.

如果我用我的脚本文件的实际内容替换标签然后它工作,所以问题不是脚本本身。

If I replace the tag with the actual contents of my script file then it works, so the problem is not the script itself.

仍在寻找确认和其他想法来规避。我不方便将脚本本身修补到html中是不方便的,但到目前为止这是我最好的解决方案。或者,我可以将html写入文件系统并通过loadRequest加载,但同样不是我的第一选择。

Still looking for confirmation and additional ideas to circumvent. It's inconvenient for me to have to patch in the script itself into the html, but this is my best solution thus far. Alternatively I could write the html to the filesystem and load via loadRequest, but again, not my first choice.

UPDATE 2

感谢@djromero我有一个解决方案。我的文档是一个XHTML文档,因此使用了一个自动关闭脚本标记(没有内容,只是属性。)但是 loadHTMLString:baseURL:显然假设MIMEType为 text / html ,UIWebView显然现在更严格地解释 - 在 text / html 文档中,您可能没有自动关闭标记。

Thanks to @djromero I have a solution. My document is a XHTML document and as such used a self-closing script tag (no content, just attributes.) But loadHTMLString:baseURL: apparently assumes a MIMEType of text/html, which the UIWebView apparently now interprets more strictly - and in text/html documents you may not have self closing tags.

我的解决方案是切换到 loadData:MIMEtype:baseURL:并指定 application / xhtml + xml 作为mime类型。我可以使用 dataUsingEncoding:轻松地从我的NSString构造NSData。

My solution is to switch to loadData:MIMEtype:baseURL: and specify application/xhtml+xml as the mime type. I can easily construct the NSData from my NSString using dataUsingEncoding:.

推荐答案

我不是HTML标准专家,但是......你试图关闭 < script> tag:

I'm not an HTML standards expert, but...did you try to close the <script> tag:



<script type="text/javascript" src="./myscript.js"></script>

这对我有用。

这篇关于UIWebView loadHTMLString在iOS5中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆