iOS 静态库.a制作以及打包bundle文件
好久没有写文章了,最近公司用到了制作静态库相关的知识,在实际应用中总结了一些经验,写下来和大家分享一下,有什么不足的地方,望大家提意见以便更好的改进。
文章主要介绍
1、静态库制作规范
2、静态库制作流程
3、静态库打包bundle文件
4、合并静态库真机和模拟器文件
静态库应用场景
在工作中,我们会集成一些第三方的静态库,我们的开发的功能也会提供给其他人使用,提供给别人时,我们不想暴露自己的源码,那么这就要求我们对自己的源代码进行处理,Xcode提供了打包静态库的功能,下面我给大家一步步介绍下如何制作静态库
1、静态库制作规范
静态库是提供给其他人使用的,最好做到只提供入口方法,做到一句代码调用的目的,如达不到一句代码调用目的,只暴露主要方法
2、.a静态库制作流程
1>创建一个项目,选择Cocoa Touch Static Library,如图所示

2>创建完成如图所示

3>将提前准备的项目文件及项目资源导入到SDK制作工程中

4>添加New Header Phase

5>添加完成如图所示

6>将制作静态库需要的.h文件添加到Project中,将静态库调用的头文件添加到Public中

7><静态库打包bundle文件>由于演示制作的静态库包含图片和xib文件,因此为了规范,我们需要把图片和xib文件添加到bundle中,如图添加给静态库添加bundle资源包

8>创建好之后,将图片和xib文件添加到Copy Bundle Resources中

9>由于.bundle文件属于macOX类型,所以我们需要改一些配置来适配iOS,如图所示
TARGETS ->选择bundle -> Build Settings ->Base SDK ->选择Latest iOS (iOS 11.2)

设置Build Setting 中的COMBINE_HIDPI_IMAEGS 为NO,否则Bundle中的图片就是tiff格式了。

作为资源包,仅仅需要编译就好,无需安装相关配置,设置Skip Install为YES,同样需要删除安装路径Installation Dirctory的值


到此为止bundle文件的设置完成
10> 打包工程和资源文件



11>找到源文件路径,如下图所示,到此静态库制作完成,将.libStaticSDK.a和source.bundle和头文件StaticSDK.h导入到项目中即可使用

3、合并静态库真机和模拟器文件
我们在制作静态库的时候,编译会产两个.a文件,一个适用于模拟器的,一个是用于真机的,为了开发方便我们可以使用终端命令将.a文件进行合并
lipo -create XXX/模拟器.a路径 XXX/真机.a路径 -output 合并后的文件名称.a
4-1、注意点,由于资源文件在Bundle文件中因此在使用时需注意,以下我举两个例子,一个是加载图片,一个是加载xib文件

4-2注意事项:删除bundle中plist中图中的两项,防止影响Appstore上架报错
