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 42 of 42.
-
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 -
webview
2004-08-05 05:56:46 Andrew Anderson | [Reply | View]
I am not sure what the problem could be here. I would make sure that the connections in the WebView are correct and that you have WebKit installed properly.
-
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? -
Control Drag isn't working.
2004-07-01 07:47:12 Andrew Anderson | [Reply | View]
How are you trying to Control drag?
The standard way is to hold down Control, Click the object with the mouse button (on a multi-button mouse the left button) and drag a line to the object that you want to connect it to.
Hope that helps...
-
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. -
suggestions
2004-02-08 09:46:53 Andrew Anderson | [Reply | View]
WebKit does provide certain more advanced features and I am working on an article that will focus on quite a few of them. Unfortunately given the architecture of WebKit, unless you plan on writing your own resource handlers functionality such as "dynamically ignoring certain javascript instructions" or "showing page rendering errors" are possible only within limit scope.
As for how basic the content of the article is, I wonder how it is that we can offer an advanced article about WebKit without offering the basic knowledge in the first place.
As for the trackbacks, I have no control over how they are handled and the fact is I do think it is important enough to talk about.
I can't say that I appreciate the grammar criticism. The clause in question (because the quoted section is a clause, not a sentence) is awkward for sure, but "badly formed" is an awfully extreme way to categorize it. Of course, I am sure you can all point us to your vast array of grammatically perfect published works so my editors and I can learn what real writing looks like.
-
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. -
Controller examples
2004-01-25 01:48:25 mmalc [Reply | View]
Apple has updated a number of bindings-related pages recently: <http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/index.html>. I put a bindings-based ("controller layer") version of a web browser at <http://homepage.mac.com/mmalc/CocoaExamples/ControlledWebKit.zip>. Other examples are listed at <http://homepage.mac.com/mmalc/CocoaExamples/controllers.html>. -
RE: Again?
2004-01-24 09:41:32 Derrick Story |
[Reply | View]
Not "again" on Mac Dev though. If you've already read a similar article somewhere else, that's great. But we don't have any coverage of WebKit in our archives, and I think it's important to cover. Since we publish 2-3 original features a week, I think the odds are good there'll be something more to your tastes soon.
As for O'Reilly's "quality has done nothing but go down," you are way out of line. If you look at the recent Panther books we've published, they are top notch and comprehensive. Our Mac OS X Conference is a one of a kind event and has received excellent feedback from attendees.
Mac DevCenter is O'Reilly's effort to provide free Mac information, news, weblogs, and commentary to those interested in developing for the platform, want to try their hand at developing, or enjoy a community with others who have similar interests.
If the content on this site doesn't do it for you. Fine, That's fair. We can't be everything to everyone. There are lots of good things on the Web that I'm sure are better suited to your tastes.
But don't come to my house and insult my family just because you don't like what's being served. -
RE: Again?
2004-01-24 11:42:15 josephbriggsorn [Reply | View]
lopossum was quite harsh in his criticism but he does have a glimmer of a point. The Cocoa tutorials once were in-depth and inspiring, but now they tend toward the basics. I don't mind so much since I do access those other sites you intimate, but it would be nice to get the same depth from O'Reilly since I really like the site.
Of course a zero code browser tutorial must be done but even at my basic level, I've done this on my own as soon as I installed XCode (it is that easy). What I and lopossum and others like us might appreciate more would be a deeper look at WebKit, like how it interprets and renders html content. I think a small(ish) split view html editor tutorial would be awesome. I would be very willing to read through four 4 to 6 page installments for something like that.
I hope I don't sound like I'm "insulting your family," I would just like to think that O"Reilly is capable of serving the basic, intermediate and advanced developers/hobbyists who visit their site. -
RE: Again?
2004-01-24 12:51:20 Derrick Story |
[Reply | View]
Well, there are a few things that I'd like you to keep in mind.
First, I think we can discuss this topic with civility, as you have in your post.
Next, remember this is the first article in a two-part series. The second article does drill down more. But not everyone who comes to the site is as seasoned as you and lopossum. A large part of our readership is moving from power user to developer and wants this type of information. And one of my goals is to help as many people become Mac developers as wants to.
Also, please remember that the contributions to this site come from members of our audience who are not paid very much and give freely of their time because they believe in this platform and want to help others master it too.
There is certainly the detail you want in our O'Reilly books, which we spend countless hours working on and at great expense. They are available through our Safari online service and in bookstores everywhere.
This site does not have that kind of budget. It is free. And it is dependent upon the people who contribute to it. I thank every writer who publishes on this site for his or her time. I think our audience does a great job of providing us with interesting, topical content.
Finally, I've learned that when I publish content for both beginning developers and seasoned veterans, the site is more vibrant and has more traffic. When I publish only the in-depth stuff, traffic wanes, and if it stayed that way, the site would be in danger. -
XCode please
2004-01-24 18:41:52 keath [Reply | View]
I didn't appreciate loppossum's comments either. I am grateful for the free information available at your site. I made good use of the Apache series, for example.
I've also purchased many O'reilly books, mostly Perl; but I still have an interest in Cocoa. The one thing loppossum said I would agree with is a request for more information about using XCode and it's new controller mechanism. I saw that demoed in a movie at the Apple Developer site, but no details were given.
I still have a few more Perl books to buy from your company, but I am also looking forward to an expanded book on Cocoa programming, updated for XCode. -
RE: XCode please
2004-01-24 21:02:14 Derrick Story |
[Reply | View]
Indeed, deeper into Xcode, controller and otherwise, is a great request. Consider yourself heard (although we've been looking at it anyway). The toughest part -- finding a good writer on the subject.
Suggestions anyone?
Thanks for your comments! -
RE: XCode please
2004-01-25 00:34:39 iopossum [Reply | View]
Alright, let me begin by apologizing for the harshness of my original post. I'd like to point out however that my anger came from an appreciation for this site and a fear that it's quality may be decreasing. The original articles that were once published here got me hooked, they were several pages long and dealt with problems several of us had and things many of us wanted to do. However, I honestly saw no need for another "this is a variable this is a loop" article. The web is literally full of hundreds of these explanations and its simple not worth the effort to come out with another series of these, at least in my opinion. My frustration came from the fact that I look forward to these articles (I visit this site at least every day) and to have it be something that is lasrgely superficial and "yesterday's news" is a bit disappointing. Typing "cocoa one line browser" into google yields three distinct versions of this just quickly looking through, of which one is from Cocoa Dev Central which many of the people here probably visit as well. With this example in such abundance I would expect it to be a link on this site at the most. Now, this is "part 1" so I may be jumping the gun. This is just the impression I am getting and I am sorry if I offended anyone. I honestly didn't want to make anyone angry, I own quite a bit of Oreilly books and love them. I would also like to offer my services if it is true that many here are under time/money constraints since this is one of the few living mac dev sites and I don't want to see it die or go down. Here is the first of my own series of tutorials I have begun and would be more than willing to contribute to this site instead of the one I'm currently writing for: http://members.macteens.net/francisco/tutorials/ . -
RE: XCode please
2004-01-25 17:23:26 Derrick Story |
[Reply | View]
Alright, well let's get back on track. First item, thanks iopossum for your note. No hard feelings :)
In general, I hope we're to the point where we agree to tolerate a little diversity in our articles, ranging from beginning developer to advanced programmer, plus news, commentary, and for god sakes some fun. I think we publish enough content on Mac DevCenter to accommodate these different levels.
That being said, I do want contributions from advanced programmers (always have), and I want them to show off their stuff right here. Send me your ideas in the body of an email, and I will get back to you. Almost every article I publish starts this way.
As for my part, I will continue to look for writers who have the time to produce advanced articles. As you guys know, the people who can do this are some of the busiest in the world. I'll do my best to cajole work out of them. But it's a team effort here, and I need your help. -
RE: XCode please
2004-01-25 21:28:40 Andrew Anderson | [Reply | View]
(I feel a bit like I am jumping in a little late.. unfortunately I was out of town and off the net the whole weekend... but I feel like i need to add my 2 cents)
I understand that the first article was a little basic and that it was not the most novel concept. the thing is the goal for this topic was always two articles: part one with the basics, part two with more meat. i felt it would be confusing to readers to jump right into the intracies of webkit without having a backgroud article for readers to refer to.
that being said, if there is anything that anyone is specifically looking for in part 2, let me know and I will try my best to include it in the article. i need specifics though, not something like "more technical details of webkit".
as derrick said, it's a team effort, i can't deliver what you want in an article unless i know what it that is.
andrew
-
RE: XCode please
2004-01-31 11:19:30 jbakse [Reply | View]
Hello,
I originally took a look at this tutorial thinking there was a small chance there might be a little information I could apply towards creating an javascript aware environment.
I know that this is something that probably wouldn't fit in the next article that well, but I thought this was as good a place as any to get in a request. I'd love to see an article about embedding a javascript environment.
-justin -
RE: XCode please
2004-01-28 18:38:23 keath [Reply | View]
Thanks Andrew,
I would just like to see you follow up with what you outlined at the end of the article. For example, updating the URL line.
I saw the 'Mac OS X State of the Union' video at ADC. Towards the end, it was demonstrated how to set up a browswer using WebKit very quickly. Using XCode, it was easy to set up the outlets from the UI elements to the WebView; but I don't understand how the new controller mechanism was used to do the reverse: update the UI from WebKit.
It would be great if you could explain not only WebKit, but something about how this new tool works.
-
gracias
2004-01-23 18:52:22 restiffbard [Reply | View]
Thanks for the quick tut'. It's nice to have a solid article to give someone a jumpstart on a project. Not to blow smoke but this is why O'reilly gets so many page views from me. -
BYOB
2007-01-03 23:46:55 neely3d [Reply | View]
I am using Cocoa as a novice. I have alot of scripting language skill, and a little C experience, so I'm not surprised this isn't working right off the bat for me. Here's the deal. I have gone through the steps and created my browser. It compiles with "Success". The only problem is that it doesn't open up after the compile, and there's no compiled application in the project directory. Any clues here? I'm using XCode 2.4, Component versions
Xcode IDE: 759.0
Xcode Core: 757.0
ToolSupport: 733.0
Any help is appreciated.
Cheers,
Mike






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.