Package .a static library in iOS

Package .a static library in iOS

Word count 593 Reading 625 Comments 3

1. Create a new .a static library project

Need to select the Static Librarystatic library project template to create a new project, as shown below:


New static library project


Realize the classes that need to be packaged, as shown below:


Implement the classes that need to be packaged

2. Set the header files that need to be exposed

To add Headers, the steps are: TARGET-> Build Phases->point +number -> New Headers Phase, as shown below:



Then find Heaers(0 items)a column, +add the header file by dot number, as shown below:



Then add the header files Encryption.hthat need to be public , as shown below:



After the addition is complete, it Encryption.hwill appear in Projecta column, and you need to manually drag it into the Publiccolumn to make it public, as shown below:



After dragging in, Publicthe header file in is the public header file, as shown below:


3. Set up Scheme

Choose Edit Schemeas shown below:



Select the Debugmode, as shown below:


4. Compile the simulator version of the .a static library

Select the simulator, as shown below:



After selecting the simulator, start to compile. After the compilation is successful, the Productsmiddle libEncryption.awill change from red to black, as shown in the following figure:


5. Compile the real machine version of the .a static library

Select the real machine, as shown below:



Start compiling after selecting the real machine. After compiling successfully, the Productsmiddle libEncryption.awill change from red to black.

6. Merge the static library of the analog version and the real version

Select libEncryption.a, right click to select Show in Finder, as shown below:



After entering the Finder, you can see the compiled simulator and the real machine version of the .a static library, as shown below:



Open the terminal and use the command line to merge the versions. The specific format is:

lipo -create "真机版本.a静态库路径" "模拟器版本.a静态库路径" -output "合并后的.a静态库路径"

Note that there must be a space
in between. Enter the command line in the terminal, as shown below:



After the terminal is successfully executed, a merged version of the .a static library will be generated, as shown below:


7. Test the static library

To create a new project, drag libEncryption.aand Encryption.hfile into the project, as shown below:


After ViewController.mintroducing the Encryption.hheader file and calling the Encryption.hexposed encryption method, ViewController.mthe code is as follows:

#import "ViewController.h"
#import "Encryption.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //调用Encryption中的加密方法
    NSLog(@"-----%@", [Encryption md5EncryptWithString:@"hello"]);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

The debugging window successfully outputs the printing information, as shown in the figure below:


At this point, the .a static library is finished!

Guess you like

Origin blog.csdn.net/woruosuifenglang/article/details/53930881