Feeds

A better way to build OS X preferences

Behind the interface

3 Big data security analytics techniques

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! ®

SANS - Survey on application security programs

More from The Register

next story
OpenBSD founder wants to bin buggy OpenSSL library, launches fork
One Heartbleed vuln was too many for Theo de Raadt
Got Windows 8.1 Update yet? Get ready for YET ANOTHER ONE – rumor
Leaker claims big release due this fall as Microsoft herds us into the CLOUD
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Ubuntu 14.04 LTS: Great changes, but sssh don't mention the...
Why HELLO Amazon! You weren't here last time
Patch iOS, OS X now: PDFs, JPEGs, URLs, web pages can pwn your kit
Plus: iThings and desktops at risk of NEW SSL attack flaw
Next Windows obsolescence panic is 450 days from … NOW!
The clock is ticking louder for Windows Server 2003 R2 users
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
Apple inaugurates free OS X beta program for world+dog
Prerelease software now open to anyone, not just developers – as long as you keep quiet
prev story

Whitepapers

Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Mainstay ROI - Does application security pay?
In this whitepaper learn how you and your enterprise might benefit from better software security.
Combat fraud and increase customer satisfaction
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.