如何在xcode 4.5.1上使用WebView [英] How to use WebView on xcode 4.5.1

查看:108
本文介绍了如何在xcode 4.5.1上使用WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始编程,我需要在我的应用程序中嵌入一个网站(这是空白的;我现在只想嵌入网站)。我一直在寻找它,因为下午5:00(现在是9:30 PM),我还没有找到任何东西。



我的代码需要和在什么文件我需要写吗?我必须链接到一起吗?



我使用Xcode 4.5.1和我尝试做一个Cocoa应用程序为Mac OS X (不适用于iOS)。



对不起,如果我的某些句子不清楚,但英语不是我的主要语言。



如果您需要更多信息以帮助我,请问。

解决方案

在您的AppDelegate。 h文件,请在 #import< Cocoa / Cocoa.h> 行下面添加此行:

  #import< WebKit / WebKit.h> 

并在 @property(assign)IBOutlet NSWindow *窗口; line:

  @property(assign)IBOutlet WebView * webView; 

选择您的MainMenu.xib文件。



打开其中的窗口,然后将WebView从对象库浏览器拖动到窗口中。



应该有一个代表你的UI布局左侧的 AppController 对象的图标。控制 - 从窗口拖动到您的 WebView 。 (不要控制 - 从您的文件的所有者图标拖动!)释放鼠标按钮。将出现一个上下文菜单,其中包含单词 webView



WebKit.framework 添加到您的项目中。右键单击Xco​​de窗口左侧资源列表中的Frameworks文件夹。选择添加文件到<您的项目名称>...并使用此路径选择框架: /System/Library/Frameworks/WebKit.framework p>

选择您的AppDelegate.m文件。



在您的-applicationDidFinishLaunching:方法中, / p>

  //我提供了Apple的URL,但这是你提供自己的URL 
NSURL * url = [NSURL URLWithString:@http://www.apple.com];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
[[[self webView] mainFrame] loadRequest:urlRequest];

构建并运行。出现窗口时,您应该看到它加载您在URL中描述的网页。



最后几个字:



我看到你是新来的在Stack Overflow的上下文中,是给你一个礼物,你需要尝试一些更难在网上寻找资源,我发现了两个自己,但是因为他们有点老(和开发工具看起来不同),我开始了这个答案。我想让你承诺,你会更努力地为自己找到答案。开始的一个好地方是阅读Apple自己的非常出色的文档


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.

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 trying to do a Cocoa application for Mac OS X (not for iOS).

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.

解决方案

In your AppDelegate.h file, add this line below the #import <Cocoa/Cocoa.h> line:

#import <WebKit/WebKit.h>

and add this line below the @property (assign) IBOutlet NSWindow *window; line:

@property (assign) IBOutlet WebView *webView;

Select your MainMenu.xib file.

Open the window inside it, then drag a WebView from the Object Library browser into the window. Align and size it.

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 not control-drag from your File's Owner icon!) Release the mouse button. A contextual menu should appear, containing the word webView. Select it.

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.

Select your AppDelegate.m file.

In your -applicationDidFinishLaunching: method, replace the comment with this code:

// 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.

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.

这篇关于如何在xcode 4.5.1上使用WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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