What is a Static library?

A static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.

Why should we use Static Libraries?

1. Distribution
2. Reusability.

How to create a library Project in iOS?

Static libraries in iOS has a “.a” extension. So we are going to create it.

1. Open XCode and start a new project.
2. Under iOS, select Library and “Cocoa Touch Static Library”.

This will create a nice new project for us that builds a .a file.

Lets name it “SampleStaticLibrary”, This will create a library with name “SampleStaticLibrary.a”.
Although we can change it anytime.

Now we will get a SampleStaticLibrary.h and SampleStaticLibrary.m file.

Open the SampleStaticLibrary.h file and write some function prototypes in it.

 #import <Foundation/Foundation.h>

@interface SampleStaticLibrary : NSObject
{
    
}

- (void) functionOne
- (void) functionTwo;

@end

Implement the same in SampleStaticLibrary.m file.


#import "SampleStaticLibrary.h"

@implementation SampleStaticLibrary

- (void) functionOne{
   NSLog("I am in functionOne");
}

- (void) functionTwo{
   NSLog("I am in functionTwo");
}

@end

Important : Now connect your device or “Select iOS Device” for build. Remember when you hit run, its not going to run the application
since it is a library project. Xcode will just build the project.

Now go to the Products folder in the navigator and there you can see the library file with “.a” extension.

OOOOOhhh. Now we have built a Static Library in iOS. Right click on that file and click “Show in Finder”. There you will get the
library file or you can just drag and drop the file, make sure you click “copy file in the dialog” while importing.

Important : while importing to another project, you have to include the “.h” files that were present in your library project too in your Working project. So make sure you copy all “.h” files too.

Hooray..You have built a static library in iOS. Now we are gonna use it.


//You can do this in the "ViewController.h" file.

#import "SampleStaticLibrary.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    SampleStaticLibrary *s = [[SampleStaticLibrary alloc] init];
    [s functionOne];
    
}

@end

Done.

Go on and run your project.

Please leave your valuable comments below this post.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.