cocoa应用程序嵌入浏览器控件

使用VS.net创建一个带浏览器插件的应用程序很简单,实现访问网页功能只需要添加几行代码即可。编写cocoa应用程序嵌入浏览器控件也很简单。

首先创建一个cocoa应用程序,拖动一个按钮,一个文本框,以及一个webview控件到窗体上,添加webkit.framework库的引用


在CPAppDelegate类里面添加如下代码并绑定响应控件(xcode4.3.3环境需要包含WebKit/WebKit.h头文件,以前版本是WebKit/WebView.h头文件)

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

@interface CPAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSButton *go;
@property (assign) IBOutlet NSTextField *url;
@property (assign) IBOutlet WebView *webView;

-(IBAction)go_action:(id)sender;

@end
源代码如下:

#import "CPAppDelegate.h"

@implementation CPAppDelegate

@synthesize window = _window;
@synthesize webView;
@synthesize url;
@synthesize go;

- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com.hk"]]];
    
}

-(IBAction)go_action:(id)sender
{
    
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[url stringValue]]]];
}

@end
在xib的编辑页面下设置webview控件的align方式,使得窗体大小变动时控件大小随之改变,运行效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值