Saturday, February 1, 2014

Create Simple MAC OSX application with WebView and Load URL in it


In this blog post we will see how to create a simple MAC OSX application with webview, which loads a URL in webview. This types of application are very useful when you have nicely designed web application and you want to convert it to MAC OSX app. Using this approach, you don't have to create separate OSX application. To create this application , you will need Xcode. Open the Xcode and select "Create New xCode project". Select OS X -> Application - > Cocoa Application. It will show following window.

Enter product name, Organization Name, Company Identifier, Class Prefix of your choice and click on Next. It will ask for the location to save the project. Select the location and create project. Once the project is created, you will see following type of file stricture in Xcode.

It may be different for in you based on entered class prefix by you. After this open AppDelegate.h file. Add following line after line #import <Cocoa/Cocoa.h>

#import <WebKit/WebKit.h>

After this add following line after @property (assign) IBOutlet NSWindow *window;

@property (assign) IBOutlet WebView *webView;

Once done open MainMenu.xib file. Select window as displayed in following image and add webview as show in the following image.

After that we have to add IBOutlet for that Select App delegate from the right pane and Control + Drag from it to Webview. It will show context menu with Webview. Select the webview and it will add IBOutlet. See the following image.

Now we have to load URL in Webview. Open your AppDelegate.m file and find the applicationDidFinishLaunching function and add following code to it.

NSRect frame=[NSScreen mainScreen].frame ;
[self.window setFrame:frame display:YES animate:YES];

Above lines will open the window in fullscreen mode. 

NSURL *url = [NSURL URLWithString:@""];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[[self webView] mainFrame] loadRequest:urlRequest];
[self.window setContentView:self.webView];

Above lines will load the URL in webview. You can also set title of window with

[self.window setTitle:@"title"];

When you run the app you should see following screen.

That's it, your application is ready. You can change the URL the way you want.


  1. Hi Hiren,

    I did all your described above.

    After launch I didn't get any window - just error message from the Console:
    SimpleLoadURL (it's a name of my app) [1652:303] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)'

    What's wrong?

    1. Hi Tikhomirov
      I had the same problem the way I fixed it was by adding the "WebKit.framework"

      Select your project
      Select the App target
      Seletct General Tab
      Scroll to bottom
      In Section "Linked Frameworks and Libraries"
      Click + sign
      Type WebKit in search box
      select WebKit.framework
      click OK

      Stop App, and Run Again.

      Hope it helps
      --Carlos Santana