2

我正在尝试在UIWebView. 当文件位于根目录并以这种方式加载时,一切正常:

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];

但是当我输入 inDirectory 参数时:

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html" inDirectory:@"files"]]]];

我收到此错误:

** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“ * -[NSURL initFileURLWithPath:]: nil string parameter”

我已经检查了我的目标,并且该文件列在“复制捆绑资源”列表中

4

1 回答 1

2

构建应用程序时,加载到包中的所有资源都被压缩到一个目录中。

你只需要使用:

NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:bundleURL];

[self.webView loadRequest:request];

在 Xcode 中使用以下目录结构(test.html 在 Resources 组中):

在此处输入图像描述

如果你在模拟器中安装包应用程序时查看它,它看起来像:

在此处输入图像描述

您在 Xcode 中拥有的任何“组”或文件夹都将被删除。(注意 test.html 与其他所有内容都在根目录中)

底线,当从包中加载资源时,保持简单并使用URLForResource:withExtension:

于 2013-09-09T17:07:34.540 回答