18. February 2012 · Comments Off on Objective-C – Full-screen WebView · Categories: Mac, Snippets

This is a quick-and-dirty way to write a simple locked-down web browser perfect for kiosks or other similar scenarios. This code opens a pre-defined URL in full-screen mode and works with Snow Leopard and Lion. Instead of the overhead of a full browser like Firefox, the compiled size of this code is around ~500k and is designed to serve a very simple function.


//  AppDelegate.h
//  QuickWeb
//  Please edit below to define URL, font and font size.

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

#define MY_URL          @"http://www.example.com"
#define FONT            @"Times"
#define FONTSIZE        16

@interface AppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *mainWindow;


//  AppDelegate.m
//  QuickWeb
//  Please edit "AppDelegate.h" to define URL, font and font size.

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    int windowLevel;
    NSRect screenRect;
    // Capture the main display
    if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
        NSLog( @"Couldn't capture the main display!" );
    // Get the shielding window level
    windowLevel = CGShieldingWindowLevel();
    // Get the screen rect of our main display
    screenRect = [[NSScreen mainScreen] frame];
    // Put up a new window
    mainWindow = [[NSWindow alloc] initWithContentRect:screenRect
                                                 defer:NO screen:[NSScreen mainScreen]];
    [mainWindow setLevel:windowLevel];
    [mainWindow setBackgroundColor:[NSColor blackColor]];
    [mainWindow makeKeyAndOrderFront:nil];
    // Load content view
    NSString *urlAddress = MY_URL;
	NSURL *url = [NSURL URLWithString:urlAddress];
	NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    WebView *webView = [[WebView alloc] initWithFrame:screenRect];
    [[webView preferences] setStandardFontFamily:FONT];
    [[webView preferences] setDefaultFontSize:FONTSIZE];
    [[webView mainFrame] loadRequest:requestObj];
    [mainWindow setContentView:webView];

- (void)applicationWillTerminate:(NSNotification *)notification
    [mainWindow orderOut:self];
    // Release the display(s)
    if (CGDisplayRelease( kCGDirectMainDisplay ) != kCGErrorSuccess) {
        NSLog( @"Couldn't release the display(s)!" );

- (BOOL)canBecomeKeyWindow
    return YES;


Comments closed