而在发布模式下未正确加载

而在发布模式下未正确加载

本文介绍了HTML资源已在调试模式下正确加载,而在发布模式下未正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在部分用户界面中使用HTML文件.我通过 WebView 显示这些文件.这在Xcode的调试模式下按预期工作.不幸的是,在存档应用程序之后,HTML资源不再显示.

这很奇怪,原因有很多:我可以看到文件肯定在应用程序的正确资源文件夹中,WebView不会消失或隐藏,它只是显示白色背景,好像它没有加载任何东西./p>

此观察对于我项目中的所有 HTML文件都是正确的.即使我不在UI中使用它们,但为了在可共享的HTML文件中导出一些数据,它也不起作用(在发布模式下).

我对此问题进行了研究,但没有发现任何感兴趣的东西.我的想法不多了...

解决方案

由于您没有在问题中提供代码,因此很难对其进行调试……但是从您自己的答案中,我很确定我知道问题.您将路径字符串和URL字符串视为可互换,但它们不是可互换的.

您建议这样做:

[[inspectorView mainFrame]
  loadRequest:[NSURLRequest
        requestWithURL:[NSURL
                URLWithString:
                   [[NSBundle mainBundle]
                 pathForResource:@"inspectorView"
                      ofType:@"html"
                     inDirectory:@"HTML/inspectorView"]]]];

(我添加了一些新行以使其可读.)

要调用+ [NSURL URLWithString:],您需要一个代表URL的字符串,而不是代表路径的字符串.但是-[NSBundle pathForResource:ofType:inDirectory:]返回路径.如果需要的话,可以通过+ [NSURL fileURLWithPath:isDirectory:]路径来构造URL.

但是,除非您需要支持10.5或更早版本,否则使用-[NSBundle URLForResource:withExtension:subdirectory:]而不是-[ NSBundle pathForResource:ofType:inDirectory:].

所以:

[[inspectorView mainFrame]
  loadRequest:[NSURLRequest
        requestWithURL:[[NSBundle mainBundle]
                 URLForResource:@"inspectorView"
                  withExtension:@"html"
                   subdirectory:@"HTML/inspectorView"]]];

如果您想知道为什么代码在某些情况下行得通,而在另一些情况下行不通,请考虑使用"/Applications/MyApp.app/Contents/"(我省略了"Resources/HTML/inspectorView/inspectorView.html" "(为简洁起见,因为此处无关紧要))是指当作为URL读取时.这是相对网址;要解释它,您需要使用当前的基础,敲除路径部分,然后将其替换为字符串. + [NSURL URLWithString:]恰好使用"file://localhost"作为基础,这意味着您最终会得到:

file://localhost/Applications/MyApp.app/Contents/

这恰好是一个有效的URL,实际上是您想要的文件的有效URL.尚无记录,但确实如此.在这种情况下.但是,如果在其中放置空格怎么办?然后您得到:

file://localhost/Applications/MyApp X.app/Contents/

这不是有效的网址;你想要的是这个

file://localhost/Applications/MyApp%20X.app/Contents/

当然,如果您调用+ [fileURLWithPath:isDirectory:]而不是+ [URLWithString:],这就是您所得到的.

(我在这里有点作弊.实际上,NSURL在内部分别跟踪相对URL和基础,并在您尝试访问URL时将它们组成.但是您现在可以忽略它们.)

Currently I am using HTML files for parts of my user interface. I display these files via WebView. This works as expected in debug mode in Xcode. Unfortunately, after having archived the app, the HTML resources are not being shown anymore.

That's strange for many reasons: I can see the files are definitely in the correct resources folder within the app, the WebView doesn't disappear or hide, it simply shows a white background as if it hasn't loaded anything.

This observation is true for all HTML files within my project. Even if I don't use them for UI but in order to export some data within a shareable HTML file, it doesn't work (in release mode).

I did research on this issue but didn't find anything of interest. I'm running out of ideas now...

解决方案

Since you didn't give your code in the question, it's very hard to debug it… But from your own answer, I'm pretty sure I know the problem. You're treating path strings and URL strings as interchangeable, and they're not.

You suggest this:

[[inspectorView mainFrame]
  loadRequest:[NSURLRequest
        requestWithURL:[NSURL
                URLWithString:
                   [[NSBundle mainBundle]
                 pathForResource:@"inspectorView"
                      ofType:@"html"
                     inDirectory:@"HTML/inspectorView"]]]];

(I added some newlines to make it readable.)

To call +[NSURL URLWithString:], you need a string representing a URL, not a string representing a path. But -[NSBundle pathForResource:ofType:inDirectory:] returns a path. There is a function to construct a URL from a path, +[NSURL fileURLWithPath:isDirectory:], if that's what you want.

But, unless you need to support 10.5 and earlier, it's much easier to just get a URL in the first place and not bother converting back and forth, by using -[NSBundle URLForResource:withExtension:subdirectory:] instead of -[NSBundle pathForResource:ofType:inDirectory:].

So:

[[inspectorView mainFrame]
  loadRequest:[NSURLRequest
        requestWithURL:[[NSBundle mainBundle]
                 URLForResource:@"inspectorView"
                  withExtension:@"html"
                   subdirectory:@"HTML/inspectorView"]]];

In case you're wondering why your code works in some cases but not others, consider what "/Applications/MyApp.app/Contents/" (I'm leaving off the "Resources/HTML/inspectorView/inspectorView.html" for brevity, because that's not relevant here) means when read as a URL. It's a relative URL; to interpret it, you take the current base, knock off the path portion, and replace it with the string. +[NSURL URLWithString:] happens to use "file://localhost" as the base, which means you end up with:

file://localhost/Applications/MyApp.app/Contents/

That happens to be a valid URL, and in fact the valid URL for exactly the file you wanted. That's not documented to work, but it happens to. In this case. But what if you put a space in it? Then you get:

file://localhost/Applications/MyApp X.app/Contents/

That's not a valid URL; what you want is this:

file://localhost/Applications/MyApp%20X.app/Contents/

And of course that's exactly what you get if you call +[fileURLWithPath:isDirectory:] instead of +[URLWithString:].

(I'm cheating a bit here. Actually, NSURL internally keeps track of the relative URL and the base separately, and composes them when you try to access the URL. But you can ignore that for now.)

这篇关于HTML资源已在调试模式下正确加载,而在发布模式下未正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:26