I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009


Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Friday, September 26, 2008

HTML: The Cross-Platform Solution

Trying to write a cross-platform application today can be done in either of two ways: write a separate implementation of the app on each platform using the platform's native API or write the app in a specialized cross-platform framework. Obviously, the latter is more preferable. While Qt is the API of choice for most cases today, but for those who want to write cross-platform proprietary applications without paying a fortune, there's another unexplored alternative: HTML.

Sure, HTML is, at its basic, the only choice for web development, but it can easily be applied to desktop development. Here, I'll show a Cocoa example, but the idea can also be applied in WPF and GTK+.

Note: you should be able to create some basic applications in Cocoa in order to understand what I'm talking about here.

Create a new project and add the WebKit framework to it. Add a file.html file with the desired HTML in the file. Add a Controller class (or, as some prefer it, AppController) and make an instance of WebView called view. Don't forget to import WebKit/WebKit.h into the class! Open up the MainMenu.xib and drag in a web view. Add an NSObject and, in the inspector, make its class be Controller (or AppController). Connect the Controller to the Web View as view and save it. Add the following code to the Controller in the awakeFromNib method and run your app.

NSString* file = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"html"];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:file]];
[[view mainFrame] loadRequest:request];

Update (8 March 2009): Use the setDrawsBackground:NO method to have the page's background be transparent (unless you specified it via HTML or CSS, that is).

Change the code as needed. Good luck!

Thanks to Pieter de Bie for the code.