Xcode 4.5 - OS X Cocoa应用程序 - 基本Web视图:打开时加载Google [英] Xcode 4.5 - OS X Cocoa Application - Basic Web View: Load Google when opened

查看:169
本文介绍了Xcode 4.5 - OS X Cocoa应用程序 - 基本Web视图:打开时加载Google的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常基本的OS X Cocoa应用程序,当打开时加载 http://www.google.com 。尽可能基本(没有后退或前进按钮等)。

I'm trying to create an extremely basic OS X Cocoa Application that when opens loads http://www.google.com. As basic as possible (no back or forward buttons, etc.).

我没有Xcode 4.5的经验,我找不到任何有关网络视图的教程OS X Cocoa应用程序和Xcode 4.5。我能够找到一个教程,并为iOS Web视图创建一个Web视图。

I have little experience with Xcode 4.5 and I can't find any tutorials regarding a web view for OS X Cocoa Application AND Xcode 4.5. I was able to find a tutorial and create a web view for an iOS web view. I took what I learned but didn't get very far.

这是我到目前为止做的:

Here's what I did so far:


  • 在Xcode 4.5.2中创建了新的OS X Cocoa应用程序

  • 向Window对象添加了一个WebView对象

基于iOS网络视图教程,我假设我需要添加几行代码,它应该工作?

Based on the iOS web view tutorial I assume all I need to do is add a couple lines of code and it should work?

这是我在iOS网络视图(ViewController.m)中使用的代码:

This is the code I used in the iOS web view (ViewController.m):

NSURL *myURL = [NSURL URLWithString:@"http://www.google.com"];

NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];

[myWebView loadRequest:myRequest];

任何帮助将不胜感激。

Any help would be much appreciated. I've been stuck on this all night.

推荐答案

添加 WebView 到您的主窗口,您需要确保您已将 WebKit.framework 添加到您的项目的链接框架和库,否则您会得到一个链接错误。

After adding the WebView to your main window, you'll want to make sure you've added the WebKit.framework to the Linked Frameworks and Libraries for your project otherwise you'll get a linking error.

.h:

@class WebView;

@interface MDAppDelegate : NSObject <NSApplicationDelegate>

@property (weak) IBOutlet WebView *webView;
@property (assign) IBOutlet NSWindow *window;

@end

假设您已创建 > > IBOutlet URL使用以下代码:

Assuming you've created an IBOutlet for the WebView named webView like in the code above, you can load a URL using the code below:

.m:

@implementation MDAppDelegate

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

@end

示例GitHub项目: https://github.com/NSGod/WebViewFinagler

Sample GitHub project: https://github.com/NSGod/WebViewFinagler

这篇关于Xcode 4.5 - OS X Cocoa应用程序 - 基本Web视图:打开时加载Google的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆