Mobile App Development

Static Libraries in iOS

February 26, 2013

  • admin
  • 7

You spent a whole week working on this amazing code that allows you to edit videos, add filters, and make some cool effects. You’re done with it, and you started using it in your apps.

It took you so much time to get it done that you don’t want to share your source code with anybody else. One day, a company calls and informs you that they are very interested in using your special video editing codes. Now you’re a bit confused. You don’t want to sell your precious source code, yet you want other people to be able to use it.

What do you do in such situations?

You create a library ^_^

Static vs. Dynamic Library

Before creating a library, you should keep in mind that there are two different types of libraries, static & dynamic.

This short tutorial doesn’t cover dynamic libraries because you can’t use them in any iOS project. So our main focus will be to build a static library that gets linked to your projects.

Framework vs. Library

As an iOS developer, you’ve definitely linked different Apple Frameworks to your projects. Examples of those are: UIKit.framework & Foundation.framework.

You can think of those frameworks as a set of classes, resources, and reusable codes that can be integrated in your project. You can only build a static library which has a “.a” extension. Unfortunately, static libraries lack all other kind of resources (images, XIBs, XML, etc..).

Let’s start

Enough with theory, it’s time for practice.

This tutorial will be split into two parts:

  1. Creating a new library.
  2. Using this library in a sample project.

I- Creating a new library

In this section you’ll learn how to create a new library that does some mathematical calculations. This library can be used in different iOS projects and that’s one of the goals of creating static libraries: code reusability.

Steps to follow:

  1. Create new library project
  2. Code your library
  3. Build your library
  1. Create new library project:

– Go to Xcode

–  Create a new project

–  From the left section choose: Framework & Library under iOS section.

–  Select your only choice “Cocoa Touch Static Library” and hit next.

–  Call your project “SpecialMathLibrary” and hit next.

–  Now you have successfully created a new library project.

  1. Code your library

Navigate to “SpecialMathLibrary.h” to start declaring your public methods.

It’s preferable to use class methods in this case, and here are three methods that you are going to implement later in your .m file.

And here is what the .m file would look like:

Library coding is over, and it’s now time to build it.

  1. Build your library

All what you have to do is make sure that the Scheme is set to iOS Device.

Build your project with Cmd + B.

Once it’s built, go to your Products folder and here it is, your “.a” compiled file.

Now you can copy this  “.a” file along with all “.h” files you’ve used in this project,  in this case it’s only SpecialMathLibrary.h, and store them in a folder.

That’s all concerning how to create a Library.

  1. II.    Using this library in a sample project.

Now that you have your own library created and built, it’s time to use it in a practical way. You’ll do that by creating a sample project. Assume you’re creating a math game project where you need to calculate the area of some trigonometric shapes.

–  Go to Xcode and create a new project

–  Make sure it’s a Single View Application then hit Next

–  Call your project “MathFunGame” and also hit next

This cool game is a simple app that takes input from a user, calculates the area, and prints it on the iPhone’s Screen.

For that, only the square example will be covered, as there is no need for redundancy.

-Design your interface by going to your storyboard and adding some labels, a textField, and a button.

Design your interface to look something like this:

Next, go to your “ViewController.m” and remove what’s inside the implementation section, as we are not going to implement it for this example app.

Now link the textField with your interface section of ViewController.m.

Also link the total area label, as this needs to be updated regularly.

Add a “calculateArea” method by hooking up the Calcualte button with the implementation section of ViewController.m

Now that everything is hooked up, it’s time to add your “SpecialMathLibrary” and use its methods.

Add “libSpecialMathLibrary.a” file along with “SpecialMathLibrary.h” to your project by dragging them into your left side column.

Right click them and choose “new group”. Call it “SpecialMathLibrary”.

Now import your library where you want to use it. In this case, import it in ViewController.m

Now that the header is imported, you can use the methods declared inside it.

For this case, you’ll be using areaOfSquare and passing the side length as an argument.

This can be done in 1 line of code:

Now if you try to run it on your device, it’ll give an error.

Here’s how you can fix it.

Set “Build Active Architecture Only” to “NO”

And “Valid Architectures” to armv6 armv7

It will build and run smoothly on your device, and here’s an example.

Now, you know how to create your own static library and  have seen it used in a project!


A static library becomes a necessity when you want to reuse a code in different project with no need to modify its source. It helps in protecting your precious creations while simply sharing & distributing it to others.

Leave a Comment


I followed your tutorial and when I import the libraryclass.h and the .a file into my project and try to use the class method i get “Initializing floar with an expression of incompatible type id” & “No known class method for selector…” Here is my code for the library .m file:

#import “SolarPowerCalculator.h”

@interface SolarPowerCalculator () {

+(float)wattsGeneratedWith:(float)solarPanels ofSaidAmperage:(float)amps ofSaidVoltage:(float)volts connectedInSeries:(BOOL)series;


@implementation SolarPowerCalculator

+(float)wattsGeneratedWith:(float)solarPanels ofSaidAmperage:(float)amps ofSaidVoltage:(float)volts connectedInSeries:(BOOL)series{

//Will receive [Class wattsGeneratedWith:4 ofSaidAmperage:15.5 ofSaidVoltage:9.9 connectedInSeries:TRUE]
float totalAmps;
float totalVolts;
float totalWatts;

//1 Determine operation based on connection type
if (series) {
//Multiply Volts
totalAmps = amps * solarPanels;
totalVolts = volts;
totalWatts = totalAmps * totalVolts;
} else {
//Parallel so Multiply Amps
float totalAmps = amps;
float totalVolts = volts * solarPanels;
totalWatts = totalAmps * totalVolts;
return totalWatts;
and here is the usage in the project view controller’s .m:

float libraryCalculatedTotal = [SolarPowerCalculator wattsGeneratedWith:4 ofSaidAmperage:9.90f ofSaidVoltage:15.50f connectedInSeries:TRUE];

Reply to comment

I actually got past the error…i hadnt declared the class method in the .h…duh! But Now I get ilnker errors:

ld: warning: ignoring file /Users/quique123/Documents/iphone apps/NRGEA/NRGEA/libSolarPowerCalculator.a, file was built for archive which is not the architecture being linked (i386): /Users/quique123/Documents/iphone apps/NRGEA/NRGEA/libSolarPowerCalculator.a
Undefined symbols for architecture i386:
“_OBJC_CLASS_$_SolarPowerCalculator”, referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And I already changed architectures to armv6 & 7 and I set to NO the Build Active Arch Only field.

Yes thanks Dani, I did do that and got the no known class method error out of the way. but now i get those compiler errors i posted and im not sure why?

Thank’s Dani, you made things too simple.
Thank’s Thank’s

Reply to comment

Can you present creation of “.Framework” in same you as you presented “.a”.
It will help beginner like me.

Reply to comment


itis a very hellfull tutorial. i have a query. Actully i have made a static lib of an app which is having a UI. i just want to call it by clicking another app button . is it possible to show the UI using this. please advise

Reply to comment

Hi Dani,

Thanks for sharing the tutorial.

In addition to this can you please suggest any way to add XIB files within a static library or a .embeddedframework ?

That would really help me alot !


Reply to comment