BYOB: Build Your Own Browser
by Andrew Anderson01/23/2004
There are a lot of things to like about Apple's Safari web browser -- the stylish user interface (especially the tabs!), SnapBack feature , popup blocker, Google toolbar, and of course, Safari's speed. For developers though, one of the coolest features is hidden under the hood: WebKit -- the Cocoa/Carbon framework that's the basis for Safari.
Why is WebKit worth paying attention to? Well, it's a fully documented, fully functional set of web browsing components that developers can integrate into their Cocoa/Carbon applications. WebKit gives developers the ability to make their applications much more powerful with very little added effort.
This is the first in a series of two articles describing how to develop applications using WebKit. This article will cover building a basic web browser without writing a line of code. The browser we will have at the end of this article will include just the basics, a browser frame, a location bar, and seven buttons (backwards, forwards, stop, reload, print, smaller text, and larger text). The next article will show a little bit of code that will allow us to add some advanced features to the browser. By the time we are done with both articles, we'll have constructed a browser with several advanced features, but without writing hardly any code.
Getting WebKit
Before you start building the browser, verify that the WebKit SDK is installed on your machine. The main file you are looking for is /System/Library/Frameworks. (If you have installed the Xcode Tools for Panther you should already have this installed.) If you have this file, then you're ready to go. If not, then you need to download the WebKit SDK from Apple's Developer web site. When you log in, choose the Download link, followed by the WWDC 2003 link. You want to download the WebKit SDK(v1.0).
Setting up the Project Files
First create a Cocoa Application project in Xcode via the New Project menu choice from the File menu. Once that's done, add the WebKit framework to the project so Xcode can link against the framework during the build process.
To add the framework, choose the Frameworks/Other Frameworks folder in the project navigator. This folder should contain Foundation.framework and AppKit.framework. You need to add WebKit.framework. To do this choose Add Frameworks from the Project menu and select /System/Library/Frameworks/WebKit.framework.
Once the framework has been selected, Xcode will ask about some parameters for adding this framework to the project. Before clicking Add make sure that Add To Targets contains your executable and that it's checked, that the Text Encoding is correct for your country, and that the reference type is Default.
The last step in setting up the project files is to turn off the Zero link feature. Zero link is a useful tool that allows the development of applications without having to link and recompile with each step. While it is an extremely useful feature, Zero link is more effort than it is worth for this project. To turn Zeroconf off, choose the project, open the Get Info window via the Project menu, choose the Styles tab, and deselect the Zero link checkbox.
Building the Interface
Our browser is going to have a simple interface: seven buttons, a URL line, and the view that contains the web page.

Building this interface is very straightforward. To start you need to get into Interface Builder by double-clicking the MainMenu.nib file in the resources folder of the main project. Once Interface Builder is open, you're going to start by adding the buttons.
From the Palettes menu, choose the Cocoa-Controls tab, and from this tab click-and-drag a rounded button control into the top left corner of the main window. If you have Aqua Guidelines enabled, you can use them to determine where to put the button, otherwise put it someplace in the top left corner. After the button is placed, double-click it and change its name to Back.
The last step is to anchor this button so it stays put when the window is resized. To anchor it, bring up the Information window for the button by choosing the Show Info choice from the Tools menu. Next, choose Size from the pull-down and in the Autosizing window anchor the button to the bottom and left. You do this by clicking the portion of the vertical line that is below the middle box and the portion of the horizontal line that is left of the middle box until squiggly lines appear.
Repeat the same process for the Forward, Reload, Stop, Print, Smaller, and Larger buttons, placing each on the left of the previous button. If you run out of space in your window, resize it so that it can accommodate all of the buttons.
When done, your interface should look something like this :

Now it's time to add the URL line, which you will use to specify what web resources we would like to use. To do this, choose the Cocoa-Text tab from the palette and click-and-drag an NSTextField to just below the line of buttons on our window. Once the URL line is placed, all you need to do is anchor it, the same exact way you anchored the buttons.
The last step to building the interface is to put the WebView control onto the window. To do this, choose the Cocoa Graphics Views tab from the palette. The WebView control is the one that has the Safari icon on it. Click-and-drag this view onto the window. Place it so the top left corner of the control is in the top left, just under the URL line. Next resize the WebView so that it takes up most of the interface window.
Now anchor the WebView so that it can be resized when the window is resized. To do this, once again go to the Information window and choose the Size pull-down. In the Autosizing window we attach the WebView to the sides of the window, by clicking on the portion of the horizontal and vertical lines that are inside the middle box until the squiggly lines appear inside the box.
Connecting the Interface
Now that the interface is built, it's time to connect it together. There are eight connections to make: one from the URL line to the WebView and one from each of the buttons to the WebView.
The first connection to make is from the URL line to the WebView. First click the URL line and Control-Drag a line to the WebView, making the Info window pop up. In the Info window, choose Connections from the pull-down and choose the Target/Action button. Click the takeStringURLFrom: action and click connect.
Next you're going to connect the buttons. These connections are made the same way as the URL line connection, except the buttons are connected to the following actions:
| Button | Action |
| Back | goBack: |
| Forward | goForward: |
| Reload | reload: |
| Stop | stopLoading: |
| print: | |
| Smaller | makeTextSmaller: |
| Larger | makeTextLarger: |
Once all of the connections are made, you're done. Be sure to save your interface and then quit Interface Builder.
|
Related Reading
Learning Cocoa with Objective-C |
Running the Browser
The last step is to build and test out the project. When you quit Interface Builder and are returned to Xcode, click the hammer icon on the main Project window to build your project. After that completes, check the Build Results window to make sure the build succeeded, and then run your project. This should pop up a new window that has the interface we just built in it. To test it enter a URL (make sure it includes a protocol such as http) and hit return.
The buttons in the interface behave the same way they do in a standard web browser: Back goes to the previous page, Forward goes to the next page, Reload reloads the page, Stop stops the page from loading, Print brings up the printer dialog, Smaller makes the page text smaller, and Larger makes the page text larger. The one difference between this browser and some others is that you must enter the URL followed by the enter key to load a URL, as there is no Load button.
That's All There Is to It
As you can see, building your own simple browser with WebKit is extremely easy. A simple browser only involves two steps, build an interface and then connect the interface.
Now that the basic interface is built, you have the skeleton that you can add more advanced features to. The possibility for additional features is endless and allows developers to make their own Safari-like browsers by combining Safari's features in novel ways, or by implementing their own features.
In the next article I'll cover how to take advantage of more advanced features of WebKit, including: updating the WebKit options, displaying the page titles, updating the URL line to display the current URL, displaying the load status, and spoofing other browsers.
Andrew Anderson is a software developer and consultant in Chicago who's been using and programming on the Mac as long as he can remember.
Return to the Mac DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 21 of 21.
-
Google Search Field
2008-07-28 17:56:54 Alan.Young [Reply | View]
I made the web browser accordingly, and it works fine, but i'm trying to add a google search box to it, with no luck.
here's what i have so far:
http://i343.photobucket.com/albums/o456/Alan_Young_M/Picture7.png
can someone tell me how to make that search box in the top right corner, display results from google search?
btw awesome tut.
-
Google
2008-04-09 20:58:39 MaxApps [Reply | View]
For all you asking here's how you would add a google search box.
set a variable for text box "search" to googlesearch
the make another line with.
goto URL "http://www.google.com/search?q=" & googlesearch
cause if you type http://www.google.com/search?q= in firefox (or IE or any other browser) and type your search after that, it googles it.
BAM.
hope you found this helpfull =]
-
How to build the web browser Using .NET Technology
2008-01-10 05:31:08 VinayDaga [Reply | View]
I want the complete information about that how to work for developing the web browser.
So please give me the complete info regarding this. I am waiting for the positive response from your side....
-
help me
2006-12-18 07:43:04 jacob'o'reilly [Reply | View]
Hi there, I cant get this to work i've followed the tut to the t but the browser fails to load anything. I also tried to load the example webkit minibrowser that comes with xcode tools but that also failed to load anything. Does anybody have any tips on what could be wrong. I'm running a macbook pro with osx 10.4.8 with xcode 2.2.1
Thanks for any help you can offer
-
Nevermind, silly me
2006-02-19 22:30:06 joeyslaptop [Reply | View]
I got it to work. Disregard my other post. I have got to continue on with these tutorials!
-
Connecting the buttons to the WebView doesn't work
2006-02-19 21:46:07 joeyslaptop [Reply | View]
I got the URL line to connect to the WebView but can't get the buttons to connect. I control click and drag the line from the Back button to the WebView, but the option just switches to NextKeyView in the inspector pallette. I've clicked on action, argument, enabled, etc, but they're all blank. Basically the only thing I can choose is NextKeyView
When I use a NSColorwell button thingy, I can get the goBack, goForward, etc to show up correctly. I've tried deleting my buttons and remaking them with no luck.
-
just the thing
2005-01-18 22:10:02 tf23 [Reply | View]
this was just the ticket for a "quick intro to xcode".
I'm going to jump to parts #2 and #3 now.
Keep these type of articles up and I'll soon be ready to purchase the O'Reilly Cocoa books :)
-
Parts 2 & 3
2004-12-09 16:38:12 Cassowary [Reply | View]
Just in case anyone gets here via Google or somesuch and wants parts two and three, I'll save you the trouble of doing what I did and like them for you.
Pt 2: http://www.macdevcenter.com/pub/a/mac/2004/05/28/webkit.html
Pt 3: http://www.macdevcenter.com/pub/a/mac/2004/06/04/webkit_3.html
Convenience, ay?
-
HTTPS
2004-11-04 17:25:26 klarson6869 [Reply | View]
I am having trouble getting HTTPS to work. The webkit is really cool, but if I try to navigate to a secure site like https://connect.apple.com, it just hangs forever. I AM able to go to the same site with Safari on the same computer, so its not a connection problem. Am I missing a function override or something? Please Help!!
-
webview
2004-07-30 16:40:36 conceptbravery [Reply | View]
i followed all the directions exactly. but when i type in a URL, nothing loads in the WebView box... any ideas? when i hit the print button it loads my printer dialogue... i just cant figure out the URL!
thanks for any help.
jared
-
Control Drag isn't working.
2004-07-01 03:46:19 CravenMorehead [Reply | View]
I cant figure out why I can't get my control drag in interface builder to work. I'm jumping in pretty late to this tutorial, but without interface builder I'm out of luck. Any Answers?
-
Great start
2004-04-10 00:59:39 mankin [Reply | View]
Thanks for the great start. As someone who's never used XCode before, the instructions were at just the right level.
But now I can't find part two. If it exists, can someone please post the link? If not, I eagerly await for it since I have no idea what to do next to make my browser do what I want.
-
Thanks
2004-03-10 07:24:02 rluttman [Reply | View]
OK, you hardcore developers think this is a no brainer waste of time. ME? Thanks!! Directions were perfect - except the anchoring worked when I clicked to the right of the middle box.
I am using MY browser to write this comment. First 'real' application I've written in years. It's a big deal to me. And a good way to start learning how to develop applications in OSX.
Thanks. What's next?
-
suggestions
2004-02-08 01:44:45 guet [Reply | View]
", but without writing hardly any code"
eek. Perhaps you could fix this badly formed sentence, I think you mean "while writing hardly any code." Taken literally, this means you did write lots of code.
You could ask the site-administrator to do something to avoid the repeated trackbacks from the same source too.
I have to echo what the others said here - this article is very nice, but also very basic, though it'll be interesting to see what else you do with it. For example allowing your user to dynamically ignore certain javascript instructions or images would be an interesting extension. Showing page rendering errors would also be useful for debugging user websites, stuff like that.
-
Help, Cant Find the Styles tab/Zero Checkbox
2004-01-28 17:03:22 am3 [Reply | View]
The tutorial said to "choose the project, open the Get Info window via the Project menu, choose the Styles tab, and deselect the Zero link checkbox".
I can only see a "Show Info" under the Project menu, and under that one, I do not see any "Styles tab" or any thing relating to styles.
I need to turn Zeroconfig off but don't see how.
-
Thanks, and I'm looking forward to part 2
2004-01-25 13:00:08 valiant66 [Reply | View]
I've never really done any coding, unless HTML and javascript munging for websites counts. I've certainly never built an app for Mac OS before, and I've been using Macs since System 4.3/Finder 5.1 was the latest thing.
Following your tutorial, I put my first app together in not much longer than it took to read the tutorial. Then I started fiddling, and broke it. I couldn't figure out how to back out of my changes, so I just trashed my work and built it again. Now, no doubt, I'll break it a different way.
So I think O'Reilly needs to appeal to all levels of readers -- absolute beginners included. I actually have a target function for a browser app, and the functionality hinted at for part II is needed for it. But I'm not afraid to make mistakes in the meantime, especially when it's so easy to rebuild the app from scratch.
However, I hope in a separate article O'Reilly will address "backing out" of mistakes. I've tried using CVS for some of the websites I've built, but never really figured it out. I gather SCM is something similar (or not, I'm probably wrong). I would love to know how to go back to a previous build of my app to erase the screwup I just performed. At my level of experience error messages are impenetrable: I'd rather just go back a step and try something different.
In the meantime, I'm going to go back to my app and see if I can figure out how to set up a page-load progress indicator. Thanks!
-
Thanks, I just created my first app
2004-01-24 19:30:57 sp0radic [Reply | View]
Hi
I'm a Mac OS power user, with 10 years experince on the Mac, that has never done any programming because I always found starting too difficult. This step-by-step article made me take the plunge and I now have my first app. Too easy. Hopefully this firsst step will lead me in the right direction. Thanks.
-
Thank you, thank you, thank you!
2004-01-24 13:49:28 bradltv [Reply | View]
This has been my first tow dip into Xcode and OS X programming. I know there are other comments complaining about the simplicity of your article, but it is EXACTLY what I need. I've been trying to get a progress indicator working, but to no avail. I hope you will be exploring that topic. I would also like to know how to program in a "default home page" variable that would open on program launch. Keep up the excellent work, and guffaws to /dev/null
-
Again?
2004-01-24 02:52:07 iopossum [Reply | View]
Do we really need another "Build your own browser" article. The internet became flooded with these when webkit came out and now you guys are putting it up as legitimate programing material? Please. Why don't you really delve into new technologies like you used to. There's so much new in Panther that isn't properly explained, like the controller layer for example. Oreilly is supposed to be professional, instead the quality has done nothing but go down, with a bunch of "learn C" articles and useless redundant examples like this.






(add between the { } brackets)
IBOutlet id searchQuery;(add before the @end)
- (IBAction)connectSearch:(id)sender;In the .m file:
- (void)connectSearch:(id)sender{
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
[@"http://www.google.com/search?q=" stringByAppendingString:[searchQuery stringValue]]]]];
}
Then just hook up the outlets and actions in Interface Builder to the search bar, and voilą!