Pages

Thursday, September 12, 2013

Creating a Static Library in iOS

A static library is a package of classes, functions, definitions and resources, which you can pack together and easily share between your projects.
In this tutorial you will get hands-on experience creating your own universal static library and use this .

Why Use Static Libraries?

You might want to create a static library for different reasons. For example:
    •    You want to bundle a number of classes that you and/or your colleagues in your team use regularly and share those easily around.
    •    You want to be able to keep some common code centralized so you can easily add bugfixes or updates.
    •    You’d like to share a library with a number of people, but not allow them to see your code.
    •    You’d like to make a version snapshot of a library that develops over time.
        

Getting Started

Launch Xcode, and choose File\New\Project. When the Choose a template dialog appears, select iOS\Framework & Library\Cocoa Touch Static Library, as shown below:



Click Next. In the project options dialog, type MathUtils as the product name. Then, enter a unique company identifier and make sure Use Automatic Reference Counting is checked, and Include Unit Tests is unchecked, as so:



Click Next. Finally, choose a location where you’d like to save your new project, and click Create.
Xcode has just created a ready to use static library project, and even added a class MathUtils for you.

Next, paste the following declarations below the line @interface MathUtils : NSObject

- (NSArray *) fibonacci:(NSInteger) n;
- (NSInteger) factorial:(NSInteger) n;

Nothing fancy, just our method declarations. When we distribute the library file, this header file MUST be included as well.

One thing I should point out here is don’t make every iVar public! As a newer developer you might think it’s cool to make a public property out of EVERYTHING. This is bad form and here is where it will bite you. You don’t want the external world being able to muck with things that you don’t want them to.

And Now the .m file for the implementation:

- (NSArray *) fibonacci:(NSInteger) n
{

    NSMutableArray *fib = [NSMutableArray array];

    int a = 0;
    int b = 1;
    int sum;
    int i;

    for (i=0;i < n;i++)
    {
        [fib addObject:[NSNumber numberWithInt:a]];
        sum = a + b;
        a = b;
        b = sum;
    }

    return (NSArray *) fib;
}

- (NSInteger) factorial:(NSInteger) n
{
    if ( n <= 1 )
        return 1;
    else
        return n * [self factorial:( n-1 )];

}

This code should be very familiar and that’s it for our static library.

Rather than a .app or a .ipa file, a static library has the .a extension. You can find the generated static library in the Products folder in the project navigator. Right-click or control-click libMathUtils.a and select Show in Finder in the contextual menu.




Xcode will open the folder in Finder, where you’ll see something like this:



Now drag this file into a new folder that you create where you will be bundling all of your library files. I just created a folder on my desktop named MathLibrary. Now, do the same with all of the .h files. In our case, just copy MathUtils.h into this new directory. Your directory structure should now look like:

MathLibrary

|- libMathUtils.a

|- MathUtils.h

Linking Your Library In A New Project

So now that you have built your shiny new static library, it’s time to test it out in another application. XCode has a number of ways to actually achieve this, but I will show the most simple and Mac-like… drag and drop .

Create a new View-Based project (or whatever it doesn’t really matter). I named mine MathTest.
Now, just drag this folder into the project and XCode will set up all of the linking automagically. When prompted to copy,The final step is to add the -ObjC linker flag. The linker tries to be efficient about only including needed code, which can sometimes exclude static library code. With this flag, all the Objective-C classes and categories in the library are loaded properly.You should now see the .a file along with the header files in the new project.

Click on the Build Settings tab, and locate the Other linker Flags setting, as shown below:




In the popover, click on the + button and type -ObjC, as such:



Using The Static Library Code

Now that all of the linking is set up, you just use your library like any other class. In the App Delegate class of my test project, I simple did this:

#import "MathUtils.h"

And in the applicationDidFinishLaunchingMethod…

MathUtils *mFunctions = [[MathUtils alloc] init] ;
NSLog(@"fibonacci for 10 = %@", [mFunctions fibonacci:10]);
NSLog(@"10! = %d",[mFunctions factorial:10]);

This of course prints out:

fibonacci for 10 = (
    0,
    1,
    1,
    2,
    3,
    5,
    8,
    13,
    21,
    34
) 10! = 3628800
And there it is! Static library code being executed in a totally separate project.

Hopefully this tutorial has given you some insight into the basic concepts of static libraries and how to use them in your own apps.