So last night I wondered how hard it would be to write a web browser in Cocoa. The answer is, not very hard at all. Cocoa has pretty much all the classes you need to fetch, render, and display HTML, and it all seems to be far too easy.
Its essentially ten-or-so lines of Objective-C with a NSTextView as BrowserWindow and a NSTextField as the urlText:
- (IBAction)fetchURL:(id)sender
{
NSAttributedString *rendered;
NSURL *theURL;
NSData *rawData;
theURL = [[NSURL alloc] initWithString: [URLText stringValue]];
rawData = [[NSData alloc] initWithContentsOfURL: theURL];
rendered = [[[NSAttributedString alloc] initWithHTML: rawData baseURL: theURL documentAttributes:nil] autorelease];
[BrowserWindow setRichText: TRUE];
[BrowserWindow setImportGraphics: TRUE];
[[BrowserWindow textStorage] setAttributedString: rendered];
}
And thats it! Thats the core bits of a web browser. Now I need to figure out how to make it repeat the action on the links, and CGI forms etc. I think this may require subclassing however...