macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

BYOB: Build Your Own Browser, Part 2
Pages: 1, 2, 3

Default Home Pages

If you were to run the code the way it is now, every time you created a new window, other than a JavaScript pop-up, the window would come up blank since it has not been told what page to load. To fix this we will add support for default home pages.

The first thing that needs to be done to add support for default home pages is to add method prototypes for accessors methods for our default home page variable to "MyDocument.h":


(void)setDefaultHomepage:(NSString*);
-(NSString *)getDefaultHomepage;

The next step is to add a static variable to MyDocument.m that holds the value for the default home page. At the top of the file, before the "@implementation" statement, add:


static NSString *defaultHP =@"http://www.macdevcenter.com";

We're using a static here and not a class instance variable because we want all instances of MyDocument to use the same default home page. Since Objective C does not support class variables, we need to implement it with a static variable in the class file.

The next step is to make the webView object load the default home page we specified. We will add the code to do this to "windowControllerDidLoadNib", since this method is called after the proper objects are created and before the window is displayed. The code to add this is fairly simple:


[urlString setStringValue:defaultHP];
[[webView mainFrame] loadRequest:
	[NSURLRequest requestWithURL:
	[NSURL URLWithString:defaultHP]]];

The first statement sets the value of the URL line on the UI to the default page, while the second tells the webView to load the default page.

The final step is to add the accessor methods for the home page at the end of the implementation file:


-(void)setDefaultHomepage:(NSString*)homepage{
    defaultHP = homepage;
}

-(NSString *)getDefaultHomepage{
    return defaultHP;
}

Making the URL Line Reactive

The last step for this section is to make sure that the URL line is updated when webView sends it update information. This needs to happen when a request is forwarded from one URL to another. To get the URL line to react to these, we need to use WebView's frameLoadDelegate. This delegate is called when a frame is loaded and gives information on how and what is loaded. Setting up the frameLoadDelegate is similar to setting up the UIDelegate, the first step is to tell the webView object that MyDocument will be the delegate in the windowControllerDidLoadNib method:


[webView setFrameLoadDelegate:self];

Then we add the method that the frameLoadDelegate will call when it loads the actual frame, namely:


- (void)webView:(WebView *)sender 
		didStartProvisionalLoadForFrame:
		(WebFrame *)frame

{
    // Only report feedback for the main frame.
    if (frame == [sender mainFrame]){
        NSString *url = [[[
			  [frame provisionalDataSource] 
			  request] URL] absoluteString];
        
	[urlString setStringValue:url];
    }
}

This method checks to see that this is the main frame of the window and that it sets the URL text input string to the current URL being loaded.

Once that method is added, the multi-window capabilities are now complete.

Final Thoughts

There are a number of resources available on the web for simple low-code/no-code browsers, resources for more advanced browsing applications are few and far between. Apple's site provides an article called Displaying Web Content, which provides guidance on some of WebKit's features, and on all the classes that are available.

One other interesting resource I found is the Trailblazer project at University of Illinois-Urbana Champaign. Trailblazer is a project that uses WebKit but is really more about a revolutionary way to display a user's browsing history

In next week's article, we'll tackle adding a preference window and a content eliminator. Until then, happy coding.

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