6
votes

I am just starting to develop mac apps and I want that the WebView a URL when the app start.Here's my code:

AppDelegate.h

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

@interface AppDelegate : NSObject <NSApplicationDelegate> {
     WebView *myWebView;
    //other instance variables
}

@property

(retain, nonatomic) IBOutlet WebView *myWebView;

//other properties and methods

@end

AppDelegate.m:

 #import "AppDelegate.h"
#import <WebKit/WebKit.h>

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *urlText = @"http://google.com";
    [[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
    return;
    // Insert code here to initialize your application
}

@end

How to load URL on launch in a WebView (OSX project)?

I think that the code work but when I try to connect the code with the WebView in Interface Builder,I can't find the "web view" in the list of outlets. Thanks I'have update my code following the last post but is still doesn't work. Thanks again for you replies.

2
do @synthesize myWebView; ??iPatel
@iPatel with the latest LLVM you do not need to @synthesize any more.rckoenes
Thanks for you reply!Now I can define the outlet "web view "from the app delegate menu.But is still not work:the web view doesn't load the url.Skynext
@Skynext: check the answer...need to add webkit frameworkAnoop Vaidya
I have already added (since the beginning),...Skynext

2 Answers

8
votes

You need to add WebKit framework.enter image description here

#import <WebKit/WebKit.h>

enter image description here

6
votes

Hard to be sure what the issue is here, so a guess...

Which way are you dragging the connection in IB?

To connect your outlet you want to drag from the outlet shown in the Inspector to the web view:

making the connection

If you drag in the other way, from the web view to the App Delegate in the outline you are trying to connect an action.

You also have an issue in your code, your instance variable:

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
   WebView *myWebView;
   //other instance variables
}

will not be used by your property:

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

as your property is automatically synthesised and so will create an instance variable _myWebView. You should see a compiler warning to this effect.

This in turn means the statement:

[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];

will not do what you expect, as myWebView will be nil and not refer to your WebView. You should refer to the property as self.myWebView:

[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];

With those changes you should see Google in your web view.

HTH