Package .a static library in iOS
1. Create a new .a static library project
Need to select the Static Library
static library project template to create a new project, as shown below:
Realize the classes that need to be packaged, as shown below:
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.h
that need to be public , as shown below:
After the addition is complete, it Encryption.h
will appear in Project
a column, and you need to manually drag it into the Public
column to make it public, as shown below:
After dragging in, Public
the header file in is the public header file, as shown below:
3. Set up Scheme
Choose Edit Scheme
as shown below:
Select the Debug
mode, 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 Products
middle libEncryption.a
will 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 Products
middle libEncryption.a
will 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.a
and Encryption.h
file into the project, as shown below:
After ViewController.m
introducing the Encryption.h
header file and calling the Encryption.h
exposed encryption method, ViewController.m
the 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!