在实际开发中,有时候会遇到把代码提供给第三方,为了保证后期的可维护性,这时我们可以考虑打成.a静态包的形式。
一 .a的制作
1.新建工程或是在工程中点+号,选择如图选项,Framework&Library -> Cocoa Touch Static Library。
2.将相应的.h 和 .m分别添加到选项中(注:如果项目中引用到了第三方的头文件可以将对应的framework添加到Link Binary With Libraries中)
3.选择对应的lib运行一下,就能在Products中看到(另:默认为debug环境,如果需要release版本,需要在dit Scheme,然后修改Build Configuration)
二 .bundle制作
.bundle是方便存放资源文件如xib和图片资源等
1.添加方法同添加.a类似,只是选项是在macOS中,添加之后修改COMBINE_HIDPI_IMAGES为NO(注:不修改图片格式为tiff),以及Base SDK改为iOS版本
2.添加好之后,选择对应的bundle,点击运行,即可看到对应的资源文件,同.a制作方法。
三 脚本合并
.a库是会生成模拟器和真机两个不同的包,使用起来会非常不方便,所以我们可以考虑用脚本合并生成的.a文件,具体如下图。
具体合并脚本语法可以自行百度,我这提供2个版本的脚本
代码一:
# Sets the target folders and the final framework product.
FMK_NAME=libOCRLibrary.a
# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${PROJECT_DIR}/product
# Working dir will be deleted after the framework creation.
WRK_DIR=/Users/XXXX/Developer/Xcode/DerivedData/XXXX/Build/Products(填写自己工程对应的路径)
DEVICE_DIR=${WRK_DIR}/Release-iphoneos
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator
OUTPUT=${INSTALL_DIR}/${FMK_NAME}
# Building both architectures.
#xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos
#xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator
# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
# Creates and renews the final product folder.
mkdir -p "${INSTALL_DIR}"
# Copies the headers and resources files to the final product folder.
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${OUTPUT}"
#rm -r "${WRK_DIR}"
代码二:
if [ "${ACTION}" = "build" ]
then
#要build的target名
target_Name=${PROJECT_NAME}
echo "target_Name=${target_Name}"
#build之后的文件夹路径
build_DIR=${SRCROOT}/build
echo "build_DIR=${build_DIR}"
#真机build生成的头文件的文件夹路径
DEVICE_DIR_INCLUDE=${build_DIR}/Release-iphoneos/include/${PROJECT_NAME}
echo "DEVICE_DIR_INCLUDE=${DEVICE_DIR_INCLUDE}"
#真机build生成的.a文件路径
DEVICE_DIR_A=${build_DIR}/Release-iphoneos/lib${PROJECT_NAME}.a
echo "DEVICE_DIR_A=${DEVICE_DIR_A}"
#模拟器build生成的.a文件路径
SIMULATOR_DIR_A=${build_DIR}/Release-iphonesimulator/lib${PROJECT_NAME}.a
echo "SIMULATOR_DIR_A=${SIMULATOR_DIR_A}"
#目标文件夹路径
INSTALL_DIR=${SRCROOT}/Products/${PROJECT_NAME}
echo "INSTALL_DIR=${INSTALL_DIR}"
#目标头文件文件夹路径
INSTALL_DIR_Headers=${SRCROOT}/Products/${PROJECT_NAME}/Headers
echo "INSTALL_DIR_Headers=${INSTALL_DIR_Headers}"
#目标.a路径
INSTALL_DIR_A=${SRCROOT}/Products/${PROJECT_NAME}/lib${PROJECT_NAME}.a
echo "INSTALL_DIR_A=${INSTALL_DIR_A}"
#判断build文件夹是否存在,存在则删除
if [ -d "${build_DIR}" ]
then
rm -rf "${build_DIR}"
fi
#判断目标文件夹是否存在,存在则删除该文件夹
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
#创建目标文件夹
mkdir -p "${INSTALL_DIR}"
#build之前clean一下
xcodebuild -target ${target_Name} clean
#模拟器build
xcodebuild -target ${target_Name} -configuration Release -sdk iphonesimulator
#真机build
xcodebuild -target ${target_Name} -configuration Release -sdk iphoneos
#复制头文件到目标文件夹
cp -R "${DEVICE_DIR_INCLUDE}" "${INSTALL_DIR_Headers}"
#合成模拟器和真机.a包
lipo -create "${DEVICE_DIR_A}" "${SIMULATOR_DIR_A}" -output "${INSTALL_DIR_A}"
#打开目标文件夹
open "${INSTALL_DIR}"
fi
运行,即可看到合并的.a文件了
关于脚本2的引用,参考了谦言忘语的文章 http://www.jianshu.com/p/9cf90b9537fd