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
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 27 of 27.
-
helpppp
2008-11-04 14:53:00 ali_Bas [Reply | View]
-
helpppp
2008-11-15 17:59:38 Kemper [Reply | View]
Yeah, that stopped me too. Apparently quite a few things have changed in Interfaced Builder since 2004, and they have not updated this article. I was able to get part one of this three part series working, but this whole thing about "the MyDocument class in the Classes panel" has me completely stymied. I'd be nice if they updated this article so that the tutorial could be followed again.
-
Problem
2006-03-15 17:13:19 macdude02 [Reply | View]
Ok so i followed the instructions and when I Build it, it says there is 2 errors is Document.h. they say.
MyDocument.h:25: error: parse error before '[' token
MyDocument.h:28: fatal error: method definition not in @implementation context
when i click on the first error it brings me to the code and highlights this.
[webView setUIDelegate:self];
when i click the second error is brings up the code and highlights this
- (id)webView;
here is my code so you can take a look at it....
___________________________________________________________
//
// MyDocument.h
// Acervus2
//
// Created by Ryan Beckmann on 3/15/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
[webView setUIDelegate:self];
[webView setGroupName:@"MyDocument"];
- (id)webView;
- (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*);
-(NSString *)getDefaultHomepage;
________________________________________________________
//
// MyDocument.m
// Acervus2
//
// Created by Ryan Beckmann on 3/15/06.
// Copyright __MyCompanyName__ 2006 . All rights reserved.
//
#import "MyDocument.h"
static NSString *defaultHP =@"http://www.apple.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];
[webView setUIDelegate:self];
[webView setGroupName:@"MyDocument"];
[urlString setStringValue:defaultHP];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:defaultHP]]];
[webView setFrameLoadDelegate:self];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
- (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{
- (id)webView;
[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
Here is the header:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface MyDocument : NSDocument
{
IBOutlet id urlString;
IBOutlet WebView *webView;
}
- (void)setDefaultHomepage:(NSString*) homepage;
-(NSString *)getDefaultHomepage;
- (IBAction)connectURL:(id)sender;
@end
-
I have working code
2006-02-26 17:31:10 breakbiologist [Reply | View]
//
// 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
-
Full Code
2006-02-19 18:46:17 roundobi [Reply | View]
Could someone who has the full .m and .h code, please post it or send it to me?
collins@roundobia.com
thanks
-
Window dissapeared
2005-11-28 00:00:49 Appleboy [Reply | View]
I did the tutorial, and it worked fine. Only one thing though (a kind of important thing) in the run log, it says this:
Footprint has exited with status 0.
[Session started at 2005-11-28 16:58:59 +0900.]
2005-11-28 16:58:59.605 Footprint[981] *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)
(I named it footprint =) )
And then, the window dissapeared.
Could anyone help?
Thanks, Micah
-
Fix page 2 and 3 with placement location
2005-11-09 07:22:08 am3 [Reply | View]
I see others have pointed out the shortcoming of this other wise great article also.
So why haven't the author or webmaster corrected these instructions to include the line placement of code for pages 2 and 3.
We need help.
Or if any one has got the app to work correct, can you please address this issues or post your source files so that we can all benefit and learn how this thing works.
Thanks.
-
Two Problems
2005-07-13 19:52:39 valiantsoul [Reply | View]
Well other than the obvious problem with the homepage declarations, I would like to note that at least in XCode 2.1, MyDocument in IB is a subclass of NSDocument which is a subclass of NSObject, not MyDocument being a subclass of NSObject. Also as I stated on someone else's reply, it seems to crash every so often - any thoughts? -
Two Problems
2005-07-14 06:33:47 Andrew Anderson | [Reply | View]
In terms of the object heirarchy it looks like XCode has changed slightly how you navigate the object heirarchy from previous versions (this article was written last year). When you set up a multi-document application, I believe that MyDocument should default to be a subclass of NSDocument.
Also, WebKit has definately changed. I believe it is now on revision 4.12.1, which would account for some of the strange compilation issues that you talk about in other posts. I believe the fix of including WebKit.h, forces inclusion of all the WebKit framework header files.
In terms of the crashes, a thousand things could get it to crash on occasion. I think the debugger is the best way to go to figure it out. If I had to guess, I would say that it is probably caused by WebKit trying to access one of the delegates after it has been either disregarded or set to nil.
Sorry that I do not have more specific answers, I have not tried to upgrade the BYOB browser to the latest webkit or Tiger yet.
Andrew
-
Gah!
2005-06-29 19:28:52 Abcu [Reply | View]
I'm getting errors and warnings such as:
warning: 'WebFrame' may not respond to '-loadRequest:'
warning: (Messages without a matching method signature
warning: will be assumed to return 'id' and accept
warning: '...' as arguments.)
error: wrong type argument to unary minus
warning: 'WebFrame may not respond to '-loadRequest:'
warning: 'WebFrame may not respond to '-loadRequest:'
warning: 'WebFrame may not respond to '-provisionalDataSource'.
The errror "wrong type argument to unary minus" showed up associated with line "- (id)webView;" in the following block:
- (IBAction)connectURL:(id)sender{
- (id)webView;
[urlString setStringValue:[sender stringValue]];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:
[sender stringValue]]]];
Did I do something wrong?
Here's my code:
//
// MyDocument.m
// Spitfire II
//
// Created by John on 6/29/05.
// Copyright __MyCompanyName__ 2005 . 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];
[webView setUIDelegate:self];
[webView setGroupName:@"MyDocument"];
[urlString setStringValue:defaultHP];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:defaultHP]]];
[webView setFrameLoadDelegate:self];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
- (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{
- (id)webView;
[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
Here is the header:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
@interface MyDocument : NSDocument
{
IBOutlet id urlString;
IBOutlet WebView *webView;
}
- (void)setDefaultHomepage:(NSString*) homepage;
-(NSString *)getDefaultHomepage;
- (IBAction)connectURL:(id)sender;
@end
Please help!
-
didnt find this helpful
2005-02-26 06:40:11 rayquazadude [Reply | View]
why doesnt it say where to put all the code? It was very confusing. you should probably add something that shows what the code should look like if you did it right.
-
errors
2005-02-23 10:37:50 dancedrummer [Reply | View]
please help! i get the following errors:
error: parse error before ";" token
-(void)setDefaultHomepage:(NSString*);
fatal error: method definition not in class context
-(NSString *)getDefaultHomepage;
error: parse error before "[" token
[webView setUIDelegate:self];
error: parse error before "[" token
[webView setGroupName:@"MyDocument"];
thanks in advance,
dancedrummer -
errors
2005-07-13 19:48:19 valiantsoul [Reply | View]
In your MyDocument.h, you need:
- (void)setDefaultHomepage:(NSString *)homepage;
- (NSString *)getDefaultHomepage;
and then in the MyDocument.m,
- (void)setDefaultHomepage:(NSString *)homepage
{
defaultHP = homepage;
}
- (NSString *)getDefaultHomepage
{
return defaultHP;
}
As far as the parse error, you probably missed a semicolon or missed a ] on the previous line.
-
These Aren't Discriptive Enough
2004-12-31 11:22:37 imac333 [Reply | View]
The part 2 of BYOB SUCKS. It jumps from MyDocument.m to (someplace, doesn't say).
-
Cool
2004-06-03 05:18:18 DavidO [Reply | View]
Wow. Tonight I made my first ever Cocoa app. I just read this article and the previous one, made my browser, and now I'm using it to type this message - awesome! Thanks heaps. It crashed a few minutes ago. Not sure why. I'd opened a couple of windows and was just about to browse the discussion of this article... oh well - it's working Ok at the moment. -
Cool - Can I get my hands on your working code?
2005-11-09 07:27:18 am3 [Reply | View]
So you got this to work? Can you send me your full code for both ...h and ...m docs so that I can copy and paste them to my code. My app does not work.
You can send it to me at http://am3design.com and us my contact form. I don't want to post my email here.
Thanks.
-
Shiira browser
2004-05-30 13:59:26 dscotson [Reply | View]
Slightly-off topic but I would love to see an O'Reilly article on the Shiira browser: http://hmdt-web.net/shiira/index-e.html
For those who haven't seen it, Shiira is an open source WebKit-based browser that started off as a simple test application but has now changed direction and aims "to create a browser that is better and more useful than Safari."
-
Compile warnings expected?
2004-05-30 06:46:20 jamie_young [Reply | View]
Good article, I appreciate the move into a bit more advanced topics with actual code. It seems that advanced cocoa articles are getting hard to find, especially ones done after XCode was released. All that said, I wish the advanced articles could be a bit longer with more details covering the advanced topics. I really don't mind reading articles that span more than 3 web pages. (It seems many of the O'Reilly articles I find are targeted for a max of 3-4 pages).
Anyway, on to my question:
I am getting 4 compile warnings and was wondering if they are expected or if I have an elusive typo in my code somewhere. Still being new to Cocoa, I am not sure when 'Warnings' are ok and when they should actually be ignored. ;)
/.../MyDocument.m: In function `-[MyDocument connectURL:]':
/.../MyDocument.m:68: warning: `WebFrame' may not respond to `-loadRequest:'
/.../MyDocument.m:68: warning: cannot find method `-loadRequest:'; return type `id' assumed
/.../MyDocument.m: In function `-[MyDocument webView:createWebViewWithRequest:]':
/.../MyDocument.m:81: warning: `WebFrame' may not respond to `-loadRequest:'
/.../MyDocument.m:81: warning: cannot find method `-loadRequest:'; return type `id' assumed
Thanks!
~jamie -
re: Compile warnings expected?
2004-05-30 07:50:03 Andrew Anderson | [Reply | View]
Jamie,
Glad you like the article. Good news is that a third part of the article is already in the hands of the editor and going to be published next friday, so that should make up for some of the article length.
As for your warnings, as a rule it is ok but not advisable to ignore warnings that come from XCode/GCC. Warnings come when the compiler (GCC in the default existance) is confused about something, but nothing is actually wrong with the code. They often happen because the code calls a method within in a class that GCC can't identify.
In this instance Objective C gets these warnings because "WebFrame.h" has not been included, so it can not determine if "loadRequest" is one of WebFrame's methods. You can ignore this because "loadRequest" is one of WebFrame's methods, but if you want it to go away add "#import <WebKit/WebView.h>" to "MyDocument.h".
Andrew
-
re: Compile warnings expected?
2004-05-31 17:23:38 johnts [Reply | View]
Nice article, always looking for more Cocoa tutorials.
I was also getting the warnings, and adding an #import did fix it, however I added "#import <WebKit/WebFrame.h>" Now I get another warning:
/Users/john/desktop/NewBrowser/MyDocument.m:102: warning: `WebDataSource' may not respond to `-request'
I'm guessing it's just another import. -
re: Compile warnings expected?
2004-05-31 17:32:24 Andrew Anderson | [Reply | View]
could be. if you give me the actual code that is on line 102 (or 100-105 or so) of your MyDocument.m, I can get more of an idea of what is happening...
-
small correction
2004-05-29 12:27:11 zeus [Reply | View]
The prototype for the:
(void)setDefaultHomepage:(NSString*)homepage{}
should be:
-(void)setDefaultHomepage:(NSString*)homepage;
and not:
(void)setDefaultHomepage:(NSString*);
BTW very nice article.
CU -
re:small correction
2004-05-29 17:54:49 Andrew Anderson | [Reply | View]
good catch. thanks!! -
re:small correction
2005-03-08 17:14:35 -JR- [Reply | View]
I am having a problem with your tutorial here...(you should explain the second page a bit clearer) Heres my myDocument code, and i only assumed that all the code should go in there.. Would be nice with a reply, I am getting only three errors, but i cannot run the app; What is wrong and what more do you need to see? Thanx jR.
Code:
//
// MyDocument.m
// MMapp
//
// Created by JR on 09.03.05.
// Copyright __M & M__ 2005 . All rights reserved.
//
#import "MyDocument.h"
static NSString *defaultHP =@"file:///Users/jr/Desktop/flash/pageflip_v211.html";
@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.
[urlString setStringValue:defaultHP];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:defaultHP]]];
[webView setUIDelegate:self];
[webView setGroupName:@"MyDocument"];
[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.
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.
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)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];
}
-(void)setDefaultHomepage:(NSString*)homepage{
defaultHP = homepage;
}
-(NSString *)getDefaultHomepage{
return defaultHP;
}
@end






To start, we need to choose the MyDocument class in the Classes panel of the Interface Builder control panel (it is a subclass of NSObject). Once MyDocument is chosen, go to the "Classes" pulldown menu and choose "Add Outlet to MyDocument." An information window will pop up; make sure that it is on the "Outlets" tab and add two outlets: "webView" with type "WebView" and "urlString" with type "id." Next switch to the actions tab and add an action named "connectURL."