Programming, Uncategorized

Using WebView in XCode 4.0 with Objective-C

If you are wanting to use the WebView in your XCode/Obj-C project and are a little underwhelmed and confused by Apple’s docs, as i initially was then here i can show you step by step how to get up and running very quickly and very easily.

Firstly, before we begin this little tutorial is from what i have worked out myself, i found the documentation a bit confusing myself, it tends to ramble on a lot and not give the reader much in the way of code examples as i will do here; namely it talks more about Carbon than Cocoa front ends and the example code is very obscure and out of context and fails to mention several key steps for newcomers to Cocoa/Obj-C. The following steps are what i gathered from the documentation plus a little googling.

Chances are, if your reading this then like myself you got an error when trying to run you app after placement of the WebView and further more if you get past compilation you get a runtime error of: “program received signal: SIGABRT” in the code as a sort of fix-it tab and a GDB output of: “Terminating app due to uncaught exception ‘NSInvalidUnarchiveOperationException’, reason: ‘*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)”.

The 2 most significant steps that the apple docs don’t mention are 1) stating the header file for Webkit, which if your used to working in Windows you would not import the rendering engine for a webview equivalent and may be thrown by that and further more 2) import the webkit framework into the project.

So heres the steps you should take:

1) Create your project, here i created a basic cocoa project called webber.

2) Add the WebKit.framework to your project as shown below:

On the project configuration page click the plus under the "Linked Framework and Libraries" list as shown in the figure above.
search for webkit and select the webkit framework.
The linked frameworks and libraries should look like the figure shown above.

3) Add the webView to your window, in this instance i also added a textbox and a go button:

Add your webView to your project.

4) Drag your IBOutlet connections to your AppDelegate, you will need an outlet for the navigation textbox (navbar) and one for the webView component (webber), and lastly a IBAction for the go button (go). Then at the top of the code add a header for webkit, #import <Webkit/Webkit.h>. You should have something resembling this:

Establish your IBOutlets and IBActions as shown above and add the Webkit header.

5) Now we add the implementation code to your AppDelegate.m file in the go and applicationDidFinishLaunching method:

Add the implementation code.

So the first applicationDidFinishLaunching method will set the homepage of your webView to the apple site initially and after that you can type in a url in the textbox and click go and the browser will take you to the requested site.

Simples! I hope this helped those of you who bothered to read. Good luck and have fun with it.