Friday, January 7, 2011

OS X Frameworks & Bundles 3

Here's my last example on Frameworks from Dalrymple & Hillegass (Advanced Mac OS X Programming). We have an incredibly useful Framework with a single class and one method:


#import <Cocoa/Cocoa.h>

@interface Stuff : NSObject
+ (void) doStuff;


#import "Stuff.h"

@implementation Stuff

+ (void) doStuff
NSLog(@"doing stuff.");

Make a new Xcode project: Cocoa Framework and drag in these two files under Classes. Build it. Select the SimpleFramework target, and then click on Stuff.h and change it to "public".

Then get the Inspector for the target (double-click on SimpleFramework under the Targets pane) and under the Build tab > Deployment > Installation Directory type @executable_path/../Frameworks. Build the framework.

Now, when this Framework is part of an Application, that App will have the Framework in a Frameworks directory that is accessible from one level above where the App lives. This path will allow the App to find the Framework. In this screenshot, we're looking at the package contents for SimpleApp (which we haven't built yet). You can see that this parent directory which holds MacOS > SimpleApp also contains a Frameworks directory and that will have our SimpleFramework.framework.

The next step is "prebinding" but I skipped that. We don't do that anymore. See here.

Now, we make a new Cocoa Application project (SimpleApp) and in main.m import the framework and add a line:

#import <Cocoa/Cocoa.h>
#import <SimpleFramework/Stuff.h>

int main(int argc, char *argv[])
[Stuff doStuff];
return NSApplicationMain(argc, (const char **) argv);

Now, we need to copy the Framework into the App. Drag the SimpleFramework.framework bundle into the Xcode Groups & Files pane of SimpleApp.

Select the SimpleApp target and bring up New Build Phase > New Copy Files Build Phase. You can see it as a new gray square underneath the target. Drag it so that it is above (rather than below) the "Link Binary" phase. And then drag the SimpleFramework.framework from Groups & Files into the Copy Files build phase. It looks like this:

Select the Copy Files build phase and double-click it. In the Inspector, choose Destination > Frameworks from the pulldown menu. Build and run the application from the Console:

There is a last step in the book which allows the headers to not be visible in the final application. And there is a lot of other good stuff there. I plan to get another copy when the new one comes out.

No comments: