UIWebView loadHTMLString 在 iOS5 中不起作用
也许这与这个没有回应的问题类似:loadHTMLString Not Work With iOS5?
我有一个 UIWebView,我使用 loadHTMLString:baseURL: 填充它。 HTML 小而简单,它引用一个 css 样式表和一个 javascript 脚本,这些脚本通过 baseURL: 加载,该脚本设置为应用程序包内的目录。
// 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 等。html
有一个脚本标记,如下所示:
<script type="text/javascript" src="./myscript.js" />
如果我删除脚本标记,则页面在 iOS5 中加载并呈现良好。我可以看出 css 文件(其引用方式与脚本 .js 文件相同)已加载并应用。
如果我保留脚本标签但将 myscript.js 文件完全清空,它仍然无法加载。
对我来说,这似乎是某种跨站点脚本问题 - 因为 WebView 认为它应该禁止加载脚本(事实上,不允许渲染页面??)
不知道从这里去哪里。有想法吗?
更新
这感觉越来越像跨站点脚本问题。如果我删除标签,它就可以工作,尽管没有脚本。我的所有图像都是从 baseURL 加载的,我的样式表也是如此。也就是说,我们知道 baseURL 正在工作。
如果我用脚本文件的实际内容替换标签,那么它就可以工作,所以问题不在于脚本本身。
仍在寻找确认和其他想法来规避。对我来说,必须将脚本本身修补到 html 中很不方便,但这是迄今为止我最好的解决方案。或者,我可以将 html 写入文件系统并通过 loadRequest 加载,但这不是我的第一选择。
更新2
感谢@djromero,我有了一个解决方案。我的文档是一个 XHTML 文档,因此使用了自关闭脚本标记(没有内容,只有属性。)但是 loadHTMLString:baseURL:
显然假定 MIMEType 为 text/html
,UIWebView 显然现在对其解释更加严格 - 并且在 text/html
文档中,您可能没有自闭标签。
我的解决方案是切换到 loadData:MIMEtype:baseURL:
并指定 application/xhtml+xml
作为 mime 类型。我可以使用 dataUsingEncoding: 轻松地从 NSString 构造 NSData。
Perhaps this is similar to this question, which has no responses: loadHTMLString Not Working With iOS5?
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 ] ];
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.
The html has a script tag, like this:
<script type="text/javascript" src="./myscript.js" />
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.
If I keep the script tag but make the myscript.js file completely empty it still fails to load.
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?
UPDATE
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.
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
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.
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:
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 HTML 标准专家,但是...您是否尝试关闭
标记:
它对我有用。
I'm not an HTML standards expert, but...did you try to close the
<script>
tag:It worked for me.
加载包含嵌入文件夹引用的 HTML 文件(例如“/file.js”)的方法是将其作为 URL 而不是 STRING 加载。
我使用它与引用的文件夹(不是引用的文件)一起在 Xcode 中创建一个普通的网站结构,并在嵌入的 index.htm 文件中使用 js/ 和 css/ 以及 images/ 引用,例如
UPDATE
我不不认为在加载的 HTML 字符串中引用 URI 是官方支持的。如果必须使用字符串,请在将其加载到 UIWebView 之前将所需的资源文件加载到该字符串中。
The way to load an HTML file that contains embedded folder references, such as '/file.js', is to load it as a URL rather than as a STRING.
I use this along with referenced folders (not referenced files) to create an ordinary website structure in Xcode, with js/ and css/ and images/ references in the embedded index.htm file, e.g.
UPDATE
I don't think that referencing a URI within a loaded HTML string was officially supported. If you must use a string, then load the needed resource files into the string before you load it into the UIWebView.