advertisement

Article:
  BYOB: Build Your Own Browser, Part 2
Subject:   I have working code
Date:   2006-02-26 17:31:10
From:   breakbiologist
//
// MyDocument.m
// The New Browser
//
// Created by ? on 2/24/06.
// Copyright __?__ 2006 . All rights reserved.
//


#import "MyDocument.h"


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


@implementation MyDocument


- (id)init
{
self = [super init];
if (self) {

// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message and return nil.

}
return self;
}


- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"MyDocument";
}


- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
[webView setUIDelegate:self];
[webView setGroupName:@"MyDocument"];
[urlString setStringValue:defaultHP];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:defaultHP]]];
[webView setFrameLoadDelegate:self];
}


- (NSData *)dataRepresentationOfType:(NSString *)aType
{
// Insert code here to write your document from the given data. You can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.

// For applications targeted for Tiger or later systems, you should use the new Tiger API -dataOfType:error:. In this case you can also choose to override -writeToURL:ofType:error:, -fileWrapperOfType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.


return nil;
}


- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
// Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.

// For applications targeted for Tiger or later systems, you should use the new Tiger API readFromData:ofType:error:. In this case you can also choose to override -readFromURL:ofType:error: or -readFromFileWrapper:ofType:error: instead.

return YES;
}


- (IBAction)connectURL:(id)sender{
[urlString setStringValue:[sender stringValue]];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:
[sender stringValue]]]];
}


- (id)webView
{
return webView;
}


- (WebView *)webView:(WebView *)sender
createWebViewWithRequest:
(NSURLRequest *)request
{
id myDocument = [
[NSDocumentController
sharedDocumentController]
openUntitledDocumentOfType:
@"DocumentType"
display:YES];


[[[myDocument webView] mainFrame]
loadRequest:request];

return [myDocument webView];
}


- (void)webViewShow:(WebView *)sender
{
id myDocument = [[NSDocumentController
sharedDocumentController]
documentForWindow:
[sender window]];
[myDocument showWindows];
}


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


-(NSString *)getDefaultHomepage{
return defaultHP;
}


- (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];
}
}


@end



---------------------------------



//
// MyDocument.h
// The New Browser
//
// Created by Alex Prevoteau on 2/24/06.
// Copyright __MyCompanyName__ 2006 . All rights reserved.
//



#import <Cocoa/Cocoa.h>


#import <WebKit/WebView.h>


@interface MyDocument : NSDocument
{
IBOutlet id urlString;
IBOutlet WebView *webView;
}
- (IBAction)connectURL:(id)sender;
- (id)webView;
-(void)setDefaultHomepage:(NSString*)homepage;
-(NSString *)getDefaultHomepage;
@end