如何在打开应用程序时启动网页

时间:2011-12-22 03:37:36

标签: cocoa

大家好,我需要一些帮助。我正在尝试在Xcode 4.0中创建一个mac应用程序,我希望它在启动时显示一个网页我已经添加了webkit框架并且有一些代码但它无法正常工作。代码在

之下

app delegate.m文件

#import "AppDelegate.h"

#import <WebKit/WebKit.h>

@implementation AppDelegate

@synthesize window = _window;


@synthesize WebView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *urlAddress = @"http://www.google.com/";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [[WebView mainFrame] LoadRequest:requestObj];
}

@end

和app delegate.h文件

#import <Cocoa/Cocoa.h>

 #import <WebKit/WebKit.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (assign) IBOutlet WebView *WebView;

@interface myAppDelegate : NSObject <NSApplicationDelegate> {
    WebView *myWebView;

}

@property (retain, nonatomic) IBOutlet WebView *myWebView;

@end

如果有人可以提供帮助,请尽快回复谢谢!

1 个答案:

答案 0 :(得分:0)

嗯,我假设代码没有编译? (它不应该!:))。 .h应该看起来像:

#import <Cocoa/Cocoa.h>

#import <WebKit/WebKit.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
    WebView *_myWebView; // not needed if you're using the latest xcode
}

@property (assign) IBOutlet NSWindow *window;
@property (retain, nonatomic) IBOutlet WebView *myWebView;

@end

和.m:

#import "AppDelegate.h"

#import <WebKit/WebKit.h>

@implementation AppDelegate

@synthesize window = _window;
@synthesize myWebView = _myWebView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *urlAddress = @"http://www.google.com/";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [[self.myWebView mainFrame] loadRequest:requestObj]; // note the lower case 'l' in 'loadRequest'
}

@end

这假设您已将.xib中的webview与myWebView属性相关联。