嵌入式html文件 - 什么是目录?

时间:2012-03-16 15:26:45

标签: objective-c ios

这是我的代码:

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSString *indexPath = [NSBundle pathForResource:@"iHelp" ofType:@"html" inDirectory:nil];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
}

应用程序崩溃,因为目录名称的“nil”无效(DUH!)。所以对于一个嵌入的html文件,应该去哪里?

更新:发现“webView”没有插座,虽然在.h文件中定义了一个...将关闭它,因为我相信这里有一个答案。将开启一个新问题:网点......谢谢大家!

3 个答案:

答案 0 :(得分:0)

如果它不在文件夹引用中,只需省略该参数:

[[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"html"];

您甚至可以直接访问网址:

[[NSBundle mainBundle] URLForResource:@"iHelp" withExtension:@"html"];

答案 1 :(得分:0)

如果文件iHelp在主包中,您可以检索其路径:

NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];

你也可以这样做:

NSURL *indexURL = [[NSBundle mainBundle] URLForResource:@"iHelp" withExtension:@"html"];
[webView loadRequest:[NSURLRequest requestWithURL:indexURL]];

答案 2 :(得分:0)

删除inDirectory:参数,因为它将默认为您当前的捆绑包。注意:这是一个实例方法。

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *indexPath = [bundle pathForResource:@"iHelp" ofType:@"html"];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];