1.新建.a静态库工程
需要选择Static Library
静态库工程模板新建工程,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-af72103d24e0e949.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
实现需要打包的类,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-88eaa389a9f77c39.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.设置需要暴露的头文件
添加Headers
, 步骤为:TARGET
->Build Phases
->点+
号->New Headers Phase
,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-6b448f97f0b4fefa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后找到Heaers(0 items)
一栏, 点+
号添加头文件, 如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-1f49608b0d49607b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
接着添加需要公开的头文件Encryption.h
, 如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-9ce515e1b9ea2153.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加完成后, Encryption.h
会出现在Project
一栏中, 需要手动拖入Public
一栏中进行公开, 如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-4f132dad98cdfe6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
拖入后, Public
中的头文件就是公开的头文件了, 如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-ac2c3f8fad5989a9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.设置Scheme
选择Edit Scheme
,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-e4809b0572b23efd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
选择Debug
模式,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-93c5a07eb51d4d1c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4.编译模拟器版本的.a静态库
选择模拟器,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-ef2510405123c5cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
选择模拟器后开始编译,编译成功后,Products
中的libEncryption.a
会由红色变为黑色,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-e7993118ecdbd8cd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5.编译真机版本的.a静态库
选择真机,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-23e24861258ed036.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
选择真机后开始编译,编译成功后,Products
中的libEncryption.a
也会由红色变为黑色.
6.合并模拟版本和真机版本的静态库
选择libEncryption.a
,右键选择Show in Finder
,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-4e143aab38df89d8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
进入Finder后,可以看到编译后的模拟器和真机版本的.a静态库,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-4889805a81a95c07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
打开终端,使用命令行进行版本合并,具体格式为:
lipo -create "真机版本.a静态库路径" "模拟器版本.a静态库路径" -output "合并后的.a静态库路径"
注意中间要有空格.
在终端中输入命令行,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-5665815a52163518.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
终端执行成功后会生成一个合并版本的.a静态库,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-af783ceb0022a028.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
7.测试静态库
新建工程,将libEncryption.a
和Encryption.h
文件拖入工程,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-0a903be8d278b3c6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在ViewController.m
引入Encryption.h
头文件,调用Encryption.h
暴露出来的加密方法,ViewController.m
中代码如下:
#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
调试窗口成功输出打印信息,如下图:
![技术分享图片](http://upload-images.jianshu.io/upload_images/1803339-617f74bdc3332126.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
至此,.a静态库制作完毕!