Detailed explanation and development of iOS static library

Detailed explanation and development of iOS static library

One: Introduction

1. What is a library?

Libraries are a way to share program code, and are generally divided into static libraries and dynamic libraries.

Static library: It is completely copied to the executable file when linking, and there are multiple redundant copies when it is used multiple times.

Dynamic library: It is not copied when linking, and the program is dynamically loaded into the memory by the system when it is running for the program to call. The system is only loaded once and shared by multiple programs, saving memory.

2. The difference between the two forms of .framework

As shown in the figure above, the form of the static library includes two forms: .a and .framework.

The form of dynamic library includes .dylib and .framework, among which .framework includes private .framework and system .framework.

Both static libraries and dynamic libraries have private .framework, but they are fundamentally different. The private .framework of dynamic libraries will have an opportunity to review the rules when they are listed, and they cannot be downloaded dynamically. The private .framework of a static library is similar to a encapsulated code block without too many restrictions.

3. The difference between .a and .framework in a static library

.a is a pure binary file. In addition to binary files, there are resource files in .framework. The .a file cannot be used directly, at least there is a .h file to cooperate, and the .framework file can be used directly because it contains the h file and other files

4. Advantages of static libraries

  • Realize the modularization of the program and modularize the fixed business into a static library.
  • Convenient to share code, that is, you can share your code base with others, but others can't see the implementation of your code.
  • The need to develop third-party SDKs, such as business communication between two companies, it is impossible to send the source code to another company. At this time, the private content is packaged into a static library, and others can only call the interface, but cannot know the implementation. detail.

The company's project needs to develop a set of SDKs that support WeChat Pay, Alipay, and UnionPay at the same time, which not only meets the needs of the company's project, but also needs to be provided to friendly companies for use.

Integrating three third-party payment SDKs at the same time, there are a large number of configuration conflicts that need to be resolved. This article introduces to you how to develop your own static library .a file through a demo, shares it with you, and summarizes the work at the same time.

Two: static library implementation

How to get the source code Demo

Follow the [Web Development] WeChat official account, reply [91] to receive it. Collect all the world's methods, convenient for you and me to develop , more iOS technology dry goods waiting to be received, all documents will be updated continuously, welcome to pay attention to grow together!

1. Create a new static library project

Open Xcode, click File\New\Project, select iOS\Framework & Library\Cocoa Touch Static Library to create a new static library project.

Name the project FBYSDKDemo, and then save the project to an empty directory. The static library project consists of the header file FBYSDKDemo.h and the implementation file FBYSDKDemo.m. These files will be compiled into the library itself, as shown below:

During development, in order to make the developed static library more convenient to use, you only need to allow users to import a header file to access the interface you provide, and perform data callbacks through the interface.

2. Import the header file

Import UIKit header files, which are required to create a library. When you are creating different component classes, you will add them to this file to ensure that they can be obtained by the users of the library.

Open FBYSDKDemo.h and import the header file

#import <UIKit/UIKit.h>

Click Build Phases, expand the Link Binary with Libraries section, click + to add a new framework, find UIKit.framework, and click add to add it.

If you do not combine the header files, the static library is useless. The static library compiles a set of files in which the classes and methods exist in the form of binary data. In the static library, classes are divided into two categories, one is public and the other is private and can only be accessed internally.

Next, you need to add a new phase in the build column to include all header files. In the Build Phases interface of Xcode, select Editor\Add Build Phase\Add Headers Build Phase.

Note: If you find that the menu item found above is not clickable, as shown below:

Click on the white area of the Build Phases interface below to get the Xcode application focus, and then try again

Drag FBYSDKDemo.h from the project to the Public section under Copy Headers. This is to ensure that users can use the classes or interfaces exposed in the library.

The classes added in the Public section under Copy Headers are all public. There are three groups here, Public is public, the header files under Private can be seen by users, and the files under Project are private. It is recommended to put the files under Public and Project as much as possible.

3. Add configuration

Adding configuration is mainly operated under Build Settings, click the project name, then select the FBYSDKDemo static library target, select the Build Setting column, then search for public header, double-click Public Headers Folder Path, and type the following in the pop-up view


Screenshot below:

Because you create static files for others to use, it is best to ban invalid code and debug symbols, and let users choose the parts that are beneficial to their projects. Search for Dead Code Stripping, Strip Debug Symbol During Copy, and Strip Style in the search box. The configurations are as follows:

  • Dead Code Stripping is set to NO
  • Strip Debug Symbol During Copy are all set to NO
  • Strip Style is set to Non-Global Symbols

So far, the construction of the project has been completed, select the target as Generic iOS Device, press command + B to compile, the libRWUIControls.a file in the Product directory of the project navigation bar will change from red to black, indicating that the file already exists . Right-click libRWUIControls.a and select Show in Finder, as shown in the following figure:

You can see the public FBYSDKDemo.h class in the above picture, and other implementation classes are in the form of binary in libFBYSDKDemo.a.

4. Function realization

Here we mainly implement the development of static libraries, and the functional part implements a simple functional demo as an example.

Implement the following code in the header file FBYSDKDemo.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

typedef void (^FBYSDKCompletion)(NSString *result);

@interface FBYSDKDemo : NSObject

 *  @param urltype         urltype iOS Android
 *  @param completion     urltype url 
- (void)urlType:(NSString *)urltype withCompletion:(FBYSDKCompletion)completion;


Where urltype is web page type information, urltype can be iOS or Android, and the corresponding web page url result is obtained according to urltype to call back completion.

The code in the implementation file FBYSDKDemo.m is as follows:

#import "FBYSDKDemo.h"

@interface FBYSDKDemo ()

@implementation FBYSDKDemo

- (void)urlType:(NSString *)urltype withCompletion:(FBYSDKCompletion)completion{
    if ([urltype isEqualToString:[NSString stringWithFormat:@"iOS"]]) {
        if (completion) {
    }else if ([urltype isEqualToString:[NSString stringWithFormat:@"Android"]]) {
        if (completion) {

Select the target as Generic iOS Device and press command + B to compile.

5. Merge static libraries

Select the target as Generic iOS Device. After compiling and running, right-click libRWUIControls.a, and select Show in Finder. The libFBYSDKDemo.a displayed can be displayed on the real machine. If it is run in a virtual machine, an error will be reported.

Therefore, select the target as a virtual machine (for example, iPhone 7), then compile and run, right-click libRWUIControls.a, and select libFBYSDKDemo.a displayed by Show in Finder to run in the virtual machine. If it is run in the real machine, an error will be reported.

Combination method: open the terminal, enter lipo -create the real machine library.a path simulator library.a path-output synthesis library name (you can copy the path of the module library.a, modify the name).a; just press enter You can see the newly synthesized .a file in the folder of the simulation library, as shown below:

The screenshot of the static library file after synthesis is as follows:

The libFBYSDKDemo_all.a file is the static library file, and a general static library .a is ready.

Three: static library use

1. Import the static library

Import the static library and the .h header file, pay attention to the options in the following figure:

2. Call function

The source code is as follows:

- (void)blogsBtn:(UIButton *)sender {
    if (sender.tag == 6000) {
        [FBYSDKDemo urlType:@"iOS" withCompletion:^(NSString *result) {
            [self contentURL:result];
    }else if (sender.tag == 6001) {
        [FBYSDKDemo urlType:@"Android" withCompletion:^(NSString *result) {
            [self contentURL:result];

The result is the result obtained through the SDK callback.

3. The DEMO screenshot is as follows

4. The demo source code of this article:

Static library generation demo

Static library use demo

I hope I can help you. If you have any questions, you can join the QQ technical exchange group: 668562416. If there is anything wrong or insufficient, I hope readers can provide more comments or suggestions.

If you need to reprint, please contact me, you can reprint with authorization, thank you

This article has been synchronized to personal blog: FBY