Feeds

A better way to build OS X preferences

Behind the interface

Internet Security Threat Report 2014

To use these classes, you’ll first need to get the corresponding header files NSPreferences.h and NSPreferencesModule.h. The simplest way of doing this is to use Steve Nygard’s excellent class-dump utility, which you can find here. Run it on the actual executable inside Appkit.framework and redirect the output to a temporary text file. You can then edit this file to extract the class declarations you’re interested in.

Create a new, empty Cocoa application to serve as a test bed for this stuff. You’ll typically want to invoke the preferences window in response to a menu option, but in my test application, I just used a button press in the main window. Here’s what the action procedure (inside AppController.m) looks like:

- (IBAction) showPreferences: (id) sender
{
    [NSPreferences setDefaultPreferencesClass: [AppPreferences class]];
    [[NSPreferences sharedPreferences] showPreferencesPanel];
}

Here, we’ve set up a subclass of NSPreferences called AppPreferences. This code tells NSPreferences to use that class, and then displays the preferences dialog. It’s inside AppPreferences.m that the interesting stuff happens:

- (id) init
{
    _nsBeginNSPSupport();    // MUST come before [super init]
    [super init];
    [self addPreferenceNamed: @"General" 
             owner: [GeneralPreferences sharedInstance]];
    [self addPreferenceNamed: @"HotKeys" 
             owner: [HotKeyPreferences sharedInstance]];
    return self;
}

- (BOOL) usesButtons
{
    return NO;
}

In this case, the init method (called from AppKit) adds two pages to our preference dialogs: GeneralPreferences and HotKeyPreferences. Both of these are sub classed from NSPreferencesModule.

Two points: first, in real-world code you’d typically localize the page names, “General” and “HotKeys” in this case. Second, by default, NSPreferences will assume the needed NIB files and image files have the same name as the module class, such as GeneralPreferences.nib or GeneralPreferences.png for example. Image files should be 32×32 pixels in size.

You’ll also note that evil-looking call to _nsBeginNSPSupport. This is a fairly recent addition by Apple that is designed, I suspect, to prevent third-party use of NSPreferences. Without a preceding call to this extern void routine, the call to super init will return nil.

A very simple demo project can be downloaded here.

There’s more that can be said about NSPreferences and NSPreferencesModule, of course, but I’ll mention just one other interesting feature of NSPreferences for now. When you call the init method for this class, it will look in your application’s resource path for a file called PreferencePanels.plist. If found, this file is used to add all your application’s preference panels in one go. For more on this, have a peek at the aforementioned PreferencePanels.plist file inside your Mail.app.

Until next time, have fun! ®

Secure remote control for conventional and virtual desktops

More from The Register

next story
That dreaded syncing feeling: Will Microsoft EVER fix OneDrive?
Microsoft's long history of broken Windows sync
Mozilla, EFF, Cisco back free-as-in-FREE-BEER SSL cert authority
Let’s Encrypt to give HTTPS-everywhere a boost in 2015
SLURP! Flick your TONGUE around our LOLLIPOP – Google
Android 5 is coming – IF you're lucky enough to have the right gadget
Nokia's N1 fondleslab's HIDDEN BRILLIANCE: The 'Z Launcher'
Sugarcoating Android's Lollipop makes tab easier to swallow
Bug fixes! Get your APPLE BUG FIXES! iOS and OS X updates right here!
Yosemite fixes Wi-Fi hiccup, older iOS devices get performance boost
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Facebook, working on Facebook at Work, works on Facebook. At Work
You don't want your cat or drunk pics at the office
Soz, web devs: Google snatches its Wallet off the table
Killing off web service in 3 months... but app-happy bonkers are fine
Meet Windows 10's new UI for OneDrive – also known as File Explorer
New preview build continues Redmond's retreat to the desktop
prev story

Whitepapers

Choosing cloud Backup services
Demystify how you can address your data protection needs in your small- to medium-sized business and select the best online backup service to meet your needs.
A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.
New hybrid storage solutions
Tackling data challenges through emerging hybrid storage solutions that enable optimum database performance whilst managing costs and increasingly large data stores.