Breaking News
Loading...
Friday, March 1, 2013

Static Libraries in iOS

9:44 PM

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!
Summary:
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.

0 comments:

 
Toggle Footer