我试图查看存储在我的 Bundle(在我的支持文件中)中的 .html 文件(index.html)。
.html 文件位于名为 HTML 的文件夹中。我的代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
_viewWeb.delegate = self;
NSString *path = [[NSBundle mainBundle]
pathForResource:@"index" ofType:@"html" inDirectory:@"HTML"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_viewWeb setScalesPageToFit:YES];
[self.viewWeb loadRequest:request];
}
我的头文件如下所示:
@interface D6ViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *viewWeb;
}
@property (weak, nonatomic) IBOutlet UIWebView *viewWeb;
@end
我将属性合成为
viewWeb = _viewWeb
.持有 UIWebView 的 View Controller 加载正常,但显示没有网页的白屏。我已经在 IB 中设置了网点。有任何想法吗?谢谢,
最佳答案
您正在使用相对路径(因此文件夹的颜色为蓝色)。你实际上可以在这里找到这个问题的答案 Load resources from relative path using local html in uiwebview或以下:
将资源拖到您的 xcode 项目中,您将获得两个选项“为任何添加的文件夹创建组”和“为任何添加的文件夹创建文件夹引用”。选择“创建文件夹引用..”选项。
下面的代码应该可以工作。
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"/HTML"]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
关于ios - UIWebView 未加载本地 .html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17430229/