如何在 xcode 4.5.1 上使用 WebView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13831896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use WebView on xcode 4.5.1
提问by Cedric Berger
I'm new to programming and I need to embed a website in my application (which is blank; I only want to embed the website right now). I've been searching for it since 5:00 PM (It's now 9:30 PM) and I still didn't find anything about that.
我是编程新手,我需要在我的应用程序中嵌入一个网站(这是空白的;我现在只想嵌入网站)。我从下午 5:00(现在是晚上 9:30)开始一直在寻找它,但我仍然没有找到任何关于它的信息。
What code do I need and in what file I need to write it? What do I have to link together?
我需要什么代码以及我需要在什么文件中编写它?我必须将什么链接在一起?
I use Xcode 4.5.1 and I'm tryingto do a Cocoa application for Mac OS X (not for iOS).
我使用 Xcode 4.5.1,我正在尝试为 Mac OS X(不适用于 iOS)做一个 Cocoa 应用程序。
I'm sorry if some of my sentences are not clear, but English is not my primary language.
如果我的某些句子不清楚,我很抱歉,但英语不是我的主要语言。
If you need more information in order to help me, just ask.
如果您需要更多信息以帮助我,请询问。
回答by Extra Savtheitroad-Faire
In your AppDelegate.h file, add this line below the #import <Cocoa/Cocoa.h>
line:
在您的 AppDelegate.h 文件中,在该行下方添加以下#import <Cocoa/Cocoa.h>
行:
#import <WebKit/WebKit.h>
and add this line below the @property (assign) IBOutlet NSWindow *window;
line:
并在该行下方添加此@property (assign) IBOutlet NSWindow *window;
行:
@property (assign) IBOutlet WebView *webView;
Select your MainMenu.xib file.
选择您的 MainMenu.xib 文件。
Open the window inside it, then drag a WebView from the Object Library browser into the window. Align and size it.
打开其中的窗口,然后将 WebView 从对象库浏览器拖到窗口中。对齐并调整大小。
There should be an icon representing your AppController
object to the left of your UI layout. Control-drag from it to your WebView
inside your window. (Do notcontrol-drag from your File's Owner
icon!) Release the mouse button. A contextual menu should appear, containing the word webView
. Select it.
在AppController
UI 布局的左侧应该有一个代表您的对象的图标。按住 Control 键将其拖到WebView
您的窗口内。(不要不控制拖动从你的File's Owner
图标!)松开鼠标按钮。应出现一个上下文菜单,其中包含单词webView
。选择它。
Add the framework WebKit.framework
to your project. Right-click on your Frameworks folder in the resource list on the left side of the Xcode window. Choose "Add files to "<your project name>"... and select the framework using this path: /System/Library/Frameworks/WebKit.framework
.
将框架添加WebKit.framework
到您的项目中。右键单击 Xcode 窗口左侧资源列表中的 Frameworks 文件夹。选择“将文件添加到“<您的项目名称>”...并使用以下路径选择框架:/System/Library/Frameworks/WebKit.framework
.
Select your AppDelegate.m file.
选择您的 AppDelegate.m 文件。
In your -applicationDidFinishLaunching: method, replace the comment with this code:
在您的 -applicationDidFinishLaunching: 方法中,用以下代码替换注释:
// I provided Apple's URL, but this is where you provide your own instead.
NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[[self webView] mainFrame] loadRequest:urlRequest];
Build and run. When the window appears, you should see it load the web page you described in the URL.
构建并运行。当窗口出现时,您应该看到它加载了您在 URL 中描述的网页。
A few final words:
最后说几句:
I see that you're new here. What I've just done, in the context of Stack Overflow, is to give you a gift. You need to try a little harder looking for resources on the Web. I found two myself, but because they're a bit old (and the development tools look different enough), I embarked upon this answer. I want you to promise that you'll work harder to find answers for yourself. A great place to begin is by reading Apple's own very excellent documentation.
我看你是新来的。在 Stack Overflow 的上下文中,我刚刚所做的就是给你一份礼物。您需要更加努力地在 Web 上寻找资源。我自己找到了两个,但因为它们有点旧(而且开发工具看起来足够不同),所以我开始了这个答案。我要你保证你会更加努力地为自己寻找答案。一个很好的起点是阅读 Apple 自己非常出色的文档。
回答by hd1
Did you find the Apple tutorial on this very subject:
WebView *webview = [[WebView alloc] init]; // or initialise using the modern-equivalent of InterfaceBuilder
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];