objective-c - OS X Web View 应用程序 - 不会加载本地 .html 网页

标签 objective-c cocoa webview

我的 AppDelegate.m 文件中有以下代码,当我打开应用程序时,该文件会打开 Google。我试图更改它以打开我复制到 Xcode 项目中的 .HTML 网页,但它不起作用。我使用的是 Xcode 4.5.2。

用于打开 Google 的代码是:

@implementation MDAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    [self.webView.mainFrame loadRequest:request];
}

我尝试将其更改为(以便它加载我放入项目文件夹中的 test.html 文件,但应用程序现在显示为空白)。我错过了什么?

@implementation MDAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"test.html"]];
    [self.webView.mainFrame loadRequest:request];
}

最佳答案

只要将您的 test.html 复制到项目的根目录(确保它也复制到您的最终应用程序包,嗯? - 检查构建阶段 )您可以使用以下代码将其加载到您的 Web View 中:

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSURL* fileURL = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[self.webView.mainFrame loadRequest:request];

关于objective-c - OS X Web View 应用程序 - 不会加载本地 .html 网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14823955/

相关文章:

android - 在android中将CSS注入(inject)带有webview的站点

ios - NS_ASSUME_NONNULL_END 之后的可空性注释警告

ios - RestKit复杂而简单的JSON RKObjectMapping几乎可以工作,但是

objective-c - 是否有可用于描述人的 Cocoa 初始值设定项?

macos - 在 2016 年,如何获取文件的 Finder “Kind”?

Android WebView : Upload image at Android client side. 无法在服务器端 JavaScript 中获取文件类型

objective-c - 如何用 UINavigationController 包装 UITableView?

ios - 获取 UIBezierPath 的所有顶点

xcode - 将可变字典添加到可变字典以转换为 JSON

android - 尝试在 TabHost 中打开 WebView