iPhone OS Static Library Template for XCode

One of the easiest ways to overwhelm yourself when developing for iPhone OS is to try to manage all your code in one project. I’ve done it countless times but have finally come up with a nice solution to separating libraries out of the main project. To make it way easier, here’s an XCode template to automate most of the process.

Installation

  1. Download the iPhone OS Static Library Template from my Git Hub Projects.
  2. Extract it and move the contents to ~/Library/Application Support/Developer/Shared/Xcode/Project Templates/iamamused/iPhone OS Static Library/ (that would be the Library in your Mac OS X /Users home directory)

By placing the template project here you’ll be able to select it from the iamamused section in User Templates when creating new projects.

Creating a new Static Library project

  1. In XCode select FileNew Project…
  2. Select the iPhone OS Static Library template from the iamamused section in User Templates (See what I did there?)
  1. Enter a project name such as “MyLibraryKit” for your static library.
  2. Yipee! You now have a library all set up and ready to import into your projects (see “Including your Library in Existing Projects” below), though it doesn’t do anything yet.

Template layout

The project template comes with a few special things.

  • An include folder to store your library specific files.
  • A DemoAppDelegate and DemoViewController based on an iPhone OS View Application that already includes the library. The Demo app classes and resources are not included in the static library. They are there only to give you a quick and easy way to test your library outside your main projects. Feel free to edit the Demo target any way you please as it won’t interfere with the library target.
  • Targets for both the Demo as well as a target for a [YourProjectName]Mobile static library. The [YourProjectName]Mobile target produces a product called lib[YourProjectName].a which is the actual static library you’ll link to in your projects.

Adding Library Specific Classes

When you want to add new classes to include in your static library, remember these few things: * Add new class (.m and .h) files to the include folder in your library project. This folder will be referenced in the Header Search Paths in your other projects so if the header files are not in include then you’ll get lost of errors. * Only add new library files to the Mobile target, not the Demo target. * Don’t forget to include the new header .h files in your libraries main header file.

An Example

For example, let’s say you have created a library called MyLibraryKit and you now want to add a new subclass of UIView called MLKView. Start by right-clicking on the MyLibraryKit Framework group in the Groups & Files tree in your MyLibraryKit Xcode project window and select AddNew File…

Next, Select iPhone OSCocoa Touch ClassObjective-C class and select a subclass of UIView from the dropdown menu. In the New File dialog make sure you do the following:

  1. Set the File Name to MLKView.m
  2. Check Also create “MLKView.h”
  3. Set the Location to the include/MyLibraryKit/ folder inside your MyLibraryKit project.
  4. Add to project: MyLibraryKit
  5. Set your Targets to MyLibraryKitMobile. You can include it in Demo but Demo already includes the library so its unnecessary and the demo won’t properly represent the use of the library.
  6. Select Finish.

You’ll now have a new MLKView.m and MLKView.h file in your MyLibraryKit Framework group with the actual source files located in the include directory. Now all you have to do is #import your new MLKView.h in your MyLibraryKit.h file and the view will be part of your library.

//
//  MyLibraryKit.h
//  MyLibraryKit
//
//  Created by Jeffrey Sambells.
//  Copyright 2010 TropicalPixels. All rights reserved.
//

#import <Foundation/Foundation.h>

// Include your plugin related headers here...
#import "MLKView.h

Testing and Developing your Library

To test your library without switching projects treat the Demo app as if it were another project–actually that’s exactly what it is. The Demo app target is already setup to include the library for you so you can start developing right away.

For example, to test the MLKView from the above example, you only need to edit the loadView method in DemoViewController.m:

- (void)loadView {
    UIScreen *screen = [UIScreen mainScreen];
    // Load your primary view here for the demo
    self.view = [[MLKView alloc] initWithFrame:[screen applicationFrame]];
}

as the static library framework has already been included at the top:

#import <MyLibraryKit/MyLibraryKit.h>

If you build and run the Demo app target you’ll get a fullscreen MLKView view (which at the moment is just white since MLKView doesn’t contain any subviews or logic).

Including your Library in Existing Projects

For the purposes of these instructions I’ll assume that you’ve created a static library project called MyLibraryKit based on the iPhone OS Static Library template above.

  1. Open your iPhone app Xcode project.
  2. Drag your MyLibraryKit.xcodeproj file from the Finder and drop it into the Groups & Files tree in your app’s Xcode project window. In the confirmation dialog that appears, you can opt to copy the source into your project but depending on your setup I would highly suggest having a common place to store your libraries so you don’t end up with multiple copies everywhere. Once you’re happy with the settings click Add:
  1. Click and expand the disclosure triangle next to the MyLibraryKit.xcodeproj icon. You’ll see a list of all the products in your MyLibraryKit project (if they’re red it’s because you haven’t built the project yet–don’t worry).
  1. Click and expand the disclosure triangle next to your app’s Targets. Drag the libMyLibraryKit.a product from your MyLibraryKit.xcodeproj to your apps Target’s Link Binary With Libraries build phase as shown here.
  1. Double-click your app Target (ExampleApp shown here) to edit the properties. In the General tab of the Target Info window, click the square + button below the Direct Dependencies list. In the sheet that appears, select MyLibraryKitMobile and click Add Target.
  1. You will now see the MyLibraryKitMobile target appearing as a dependency under your app’s Target.
  1. Under the Build tab of your app’s Target Info window, edit the Header Search Paths to include the path to the include folder inside of the MyLibraryKit folder. If your path includes spaces be sure to wrap the path in “double quotes”. TIP: Make sure you edit this property for All Configurations in the dropdown, not just the active one.
  1. While in the same window (Build* tab of your app’s Target Info), also edit the Other Linker Flags to include the following flags: -ObjC and -all_load. This will allow the Objective-C categories in the MyLibraryKit static library to execute properly.
  1. Finally, build and have fun coding!