【发布时间】:2013-09-16 21:27:06
【问题描述】:
大家好,我的 iphone 应用程序中有一个本地网站,其中包含图像/css/jquery。如果我从我的电脑浏览器打开 html 文件,它会打开所有内容都会正确显示。
这是我在 uiwebview 中加载 html 内容的代码,但它不起作用。
/ NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html" inDirectory:@"HTML1"]];
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
// [_webView loadRequest:request];
// [super viewDidLoad];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html" inDirectory:@"HTML1"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[_webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL];
以上都不是有效的 ant help :) ?
【问题讨论】:
-
htmlFile实际上是否包含 test.html 的路径?除非您明确将文件复制到 HTML1 目录,否则不应使用pathForResource:ofType:inDirectory:,而应使用pathForResource:ofType: -
你好,对不起,我没有得到你..对不起,我是 pathdirectory 的新手。
-
使用
NSLog(@"%@", htmlFile);打印出htmlFile变量,并确保它不是nil。不需要将 HTML1 指定为目录,因为 Xcode 项目导航器中的组不会复制到应用程序包中,因此 test.html 将位于应用程序包的根目录中。 -
您好,路径不为零,您在根目录中是正确的。我可以看到 html 文件的路径。 NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]; NSLog(htmlFile);库/应用程序支持/iPhone模拟器/6.1/Applications/qqqqqqqqqq/test.app/test.html
-
和 uiwebview 它仍然是空白的。