6

Okay, well I am trying to learn how to develop mac apps. I have done making a web browser and all, but I really want to know how to make the WebView load a URL (lets say http://google.com/) when the apps start. How do I do that?

0x60
  • 1,096
  • 4
  • 14
  • 23

3 Answers3

12

In your app delegate, implement

-(void)applicationDidFinishLaunching:(NSNotification*) notification
{
     ....
}

This method is called automatically by the Cocoa system when the app did finish launching, quite obvious, right?

Suppose you have IBOutlet WebView*webview in your app delegate. Then all you have to do is, inside applicationDidFinishLaunching:, to call

 NSURL*url=[NSURL URLWithString:@"http://www.google.com"];
 NSURLRequest*request=[NSURLRequest requestWithURL:url];
 [[webview mainFrame] loadRequest:request];

That's all!

Yuji
  • 34,103
  • 3
  • 70
  • 88
  • Soemhow still doesn't work for me, do I need to link it in Interface builder? – Saulius Antanavicius Dec 01 '11 at 23:32
  • 1
    Yes you need to connect the `webview` outlet to the Web View in the interface. Otherwise, how the app knows what the variable `webview` in your code refers to? In a complicated app, there can be more than one WebView in the interface builder. So you need to specify one, even if there's only one. – Yuji Dec 02 '11 at 03:43
  • I can't believe you can't do this from Interface Builder. – 11684 Oct 07 '13 at 15:56
8

You could just put

[webview setMainFrameURL:@"http://www.google.com/"];
11684
  • 7,356
  • 12
  • 48
  • 71
Anonymous
  • 81
  • 1
  • 1
-1

Both of the answers should work but if your making it for mac with Xcode 4,(im not sure about the older versions). Three things, one you have to declare an IBOutlet for your webview called webview (because that is what it is in the two examples) and you have to synthesize it in the .m file. Two instead of [[webview mainFrame] loadRequest:request] it has to be [[_webview mainFrame] loadRequest:request] (because thats what its synthesized as). And last of all you need to add the webKit framework, and import it in the header file with #import

-Thats all you need to do to get it to work on mac with Xcode 4

Evsq2
  • 52
  • 4