how to write a minimal webkit browser in 37 lines of swift

January 25, 2015

Screen Shot 2015-01-25 at 10.51.49 PM

#!/usr/bin/env swift

import WebKit

let application = NSApplication.sharedApplication()
application.setActivationPolicy(NSApplicationActivationPolicy.Regular)
let window = NSWindow(contentRect: NSMakeRect(0, 0, 800, 600), styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask, backing: .Buffered, defer: false)
window.center()
window.title = "Minimal Swift WebKit Browser"
window.makeKeyAndOrderFront(window)

class WindowDelegate: NSObject, NSWindowDelegate {
    func windowWillClose(notification: NSNotification?) {
        NSApplication.sharedApplication().terminate(0)
    }
}

let windowDelegate = WindowDelegate()
window.delegate = windowDelegate

class ApplicationDelegate: NSObject, NSApplicationDelegate {
    var _window: NSWindow
    init(window: NSWindow) {
        self._window = window
    }
    func applicationDidFinishLaunching(notification: NSNotification?) {
        let webView = WebView(frame: self._window.contentView.frame)
        self._window.contentView.addSubview(webView)
        var url = NSURL(string: "https://www.google.com")
        webView.mainFrame.loadRequest(NSURLRequest(URL: url!))
    }
}

let applicationDelegate = ApplicationDelegate(window: window)
application.delegate = applicationDelegate
application.activateIgnoringOtherApps(true)
application.run()

This is little more than a copy of the code published on practicalswift, “How to write a minimal WebKit browser in 30 lines of Swift.” I made several trivial changes to the code so that it would properly execute in my Xcode environment.

  1. The shebang was cleaned up.
  2. Line 30 used to be one line (see original code here). In order to get it to work I removed the NSURL instantiation that was chained in webView.mainFrame.loadRequest and created the separate variable url right above it on line 29. I then added a “!” right after url. I’d tried to add the “!” while it was still one line, but Swift wasn’t having any of that, so I split them apart. I’m certainly no Swift expert, but long years of battling other oddball errors in other languages has trained me to begin to break apart compound function calls made with instantiations into more, but simpler, lines, if for no other reason than to isolate the problem as an aid to debugging. In this instance, just breaking them apart made the whole thing begin to work (see application capture above).

There’s a lot for me to learn in these 30-something lines, and a lot of functionality to add. This surely won’t compete with the likes of Safari or Chrome. But it does show how complete WebKit is. I can go to any website via the Google search box and see it properly rendered. Quit an nifty feature when you don’t have to write your own rendering engine.

Also, this particular block of Swift scripting is bound to the Mac and OS X runtime environment. Ain’t no way this is going to be portable to anything else other than a Mac running OS X.

One response to how to write a minimal webkit browser in 37 lines of swift