I'm writing an iPhone app with a UIWebView which should display various html files I have in the app resource folder. In xcode my project overview, these html files are displayed like this:
我正在编写一个带有UIWebView的iPhone应用程序,它应该显示我在应用程序资源文件夹中的各种html文件。在xcode my project overview中,这些html文件如下所示:
dirA
|---> index.html
|---> a1.html
|---> a2.html
|---> my.css
|---> dirB
|---> b1.html
|---> b2.html
|---> dirC
|---> c1.html
|---> c2.html
These resources where added to the project as such:
在项目中增加的这些资源如下:
- Checked "Copy items into destination groups folder (if needed)".
- 检查“将项目复制到目标组文件夹(如果需要)”。
- Reference type: Default.
- 引用类型:默认。
- Text encoding: Unicode (utf-8).
- 文本编码:Unicode(utf - 8)。
- Recursively create groups for any added folders.
- 递归地为任何添加的文件夹创建组。
The links in my html are relative, meaning they look like this:
我的html中的链接是相对的,这意味着它们看起来是这样的:
<a href="a1.html">a1</a>
<a href="a2.html">a2</a>
<a href="dirB/b2.html">b2</a>
<a href="dirC/c1.html">c1</a>
In order to display the index.html when the app starts up, I use the following code:
以显示索引。当应用程序启动时,我使用以下代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
This works fine. Following links from the index file also works fine, as long as the html files requested are directly under dirA. If the link followed points to a file in a sub-directory, then didFailLoadWithError
will catch the situation and report that the requested file does not exist.
这是很好。只要请求的html文件直接位于dirA之下,那么遵循索引文件的链接也可以很好地工作。如果后面的链接指向子目录中的一个文件,那么didFailLoadWithError将捕获情况并报告所请求的文件不存在。
Also,
同时,
[webView loadHtmlString:myHtml];
cannot be part of the solution, as I need back and forward buttons to work in my web view.
不能成为解决方案的一部分,因为我需要后退和前进按钮在我的web view中工作。
So the question is: How can I follow a relative link to an html file in a sub directory within my resources?
所以问题是:如何在我的资源中的子目录中跟踪到html文件的相关链接?
I've been all over * and the rest of the tubes for the past few days trying to figure this one out, but nowhere have I come across the solution to this exact problem. Any insight at all would be very, very much appreciated!
在过去的几天里,我一直在*和其他管道上寻找这个问题的答案,但是我没有找到解决这个问题的方法。任何见解都将非常非常感激!
EDIT: Yoohoo! I figured it out! What joy! Here is what I did:
编辑:喂! !我想出来!什么快乐!我所做的是:
- Imported my resources anew, choosing "Create folder references for any added folders" instead of "Recursively create groups for any added folders."
- 重新导入我的资源,选择“为任何添加的文件夹创建文件夹引用”,而不是“为任何添加的文件夹递归地创建组”。
- Specified the root directory for resource, like so:
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"dirA"];
- 指定资源的根目录,如:NSString *path = [[NSBundle mainBundle] pathForResource:@"index"类型:@"html" inDirectory:@"dirA"];
3 个解决方案
#1
13
To create folders in the app bundle drag the folder to Xcode and select the radio button: "Create Folder References for any added folders".
要在app bundle中创建文件夹,请将文件夹拖到Xcode并选择单选按钮:“为任何添加的文件夹创建文件夹引用”。
#2
2
The subdirectories you use in XCode are groups not actual folders. all of the resources are likely being flattened out to the output folder. If you create actual folders outside of XCode that might work. Try creating the folder and file heirarchy and drag/dropping into XCode. Also check your build folder using Finder to see exactly how XCode is deploying your files.
在XCode中使用的子目录是组而不是实际的文件夹。所有的资源都可能被压缩到输出文件夹中。如果您在XCode之外创建实际的文件夹,那么它可能会工作。尝试创建文件夹和文件,并将其拖放到XCode中。还可以使用Finder检查构建文件夹,看看XCode是如何部署文件的。
#3
2
I managed to use HTML files containing resources and links using relative paths by using the +fileURLWithPath:
initializer instead of +URLWithString:
when loading an HTML string and passing a baseURL in the web view.
通过使用+fileURLWithPath: initializer而不是+URLWithString:加载HTML字符串并在web视图中传递baseURL时,我成功地使用了包含资源和链接的HTML文件。
NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"HTMLContent"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
"HTMLContent" is my equivalent for your "dirA". No other changes were required.
“HTMLContent”是我对你的“dirA”的对等词。不需要其他更改。
#1
13
To create folders in the app bundle drag the folder to Xcode and select the radio button: "Create Folder References for any added folders".
要在app bundle中创建文件夹,请将文件夹拖到Xcode并选择单选按钮:“为任何添加的文件夹创建文件夹引用”。
#2
2
The subdirectories you use in XCode are groups not actual folders. all of the resources are likely being flattened out to the output folder. If you create actual folders outside of XCode that might work. Try creating the folder and file heirarchy and drag/dropping into XCode. Also check your build folder using Finder to see exactly how XCode is deploying your files.
在XCode中使用的子目录是组而不是实际的文件夹。所有的资源都可能被压缩到输出文件夹中。如果您在XCode之外创建实际的文件夹,那么它可能会工作。尝试创建文件夹和文件,并将其拖放到XCode中。还可以使用Finder检查构建文件夹,看看XCode是如何部署文件的。
#3
2
I managed to use HTML files containing resources and links using relative paths by using the +fileURLWithPath:
initializer instead of +URLWithString:
when loading an HTML string and passing a baseURL in the web view.
通过使用+fileURLWithPath: initializer而不是+URLWithString:加载HTML字符串并在web视图中传递baseURL时,我成功地使用了包含资源和链接的HTML文件。
NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"HTMLContent"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
"HTMLContent" is my equivalent for your "dirA". No other changes were required.
“HTMLContent”是我对你的“dirA”的对等词。不需要其他更改。