0

I have a web app, all its resources are contained in a folder named "war".I've imported these into my xcode project (via File -> "Add Files to 'my project'").

Now my xcode project looks like:

MyProject
    |- war
        |- foo.html
    |- AppDelegate.h
    |- AppDelegate.m
    ...

so now I try loading like:

NSBundle *bundle = [NSBundle mainBundle];
NSURL *url = [NSURL fileURLWithPath:[bundle pathForResource:@"foo" ofType:@"html"] isDirectory:NO];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[self.mWebView loadRequest:req];

I don't get compile errors, but my UIWebView is empty. I do get webViewDidLoad callbacks too. Is there some other way we need to load local pages like this?

Thanks

4

2 回答 2

4

如果它是一个真正的子目录(即,文件夹名称显示为蓝色):

在此处输入图像描述

然后你可以使用URLForResource:withExtension:subdirectory:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"foo" 
                                     withExtension:@"html"
                                      subdirectory:@"war"];

或者,如果它只是一个“组”(即,如果“文件夹”没有以蓝色显示):

在此处输入图像描述

然后你使用普通的 old URLForResource:withExtension:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"foo" 
                                     withExtension:@"html"];
于 2012-12-10T15:52:08.070 回答
0

这就是我通常毫无问题地加载本地 html 文件的方式。

[webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:self.fileName withExtension:nil]]]

由于您的代码对我来说大部分看起来都是正确的,我认为这可能是其他一些问题。我建议为您的 webView 设置一个委托并检查其- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error方法以查看是否提供更多信息。

还要确保您foo.html是目标成员。

于 2012-12-10T14:47:19.883 回答