framework动态库和静态库的生成和使用(三)
iOS动态库和静态库知识专题导航
3.复杂.a静态库的生成和使用(包含Bundle文件和Category分类文件)
framework简介
framework分为动态库和静态库两种。动态framework苹果已经开放,但是开发者开发过程中只能运用系统自带的系统动态framework,例如UIKit、Foundation等;开发者开发过程中不能运用自定义的动态库,当然在开发调试过程中可以用,但是提交后在苹果审核时,如果用了自定义的动态库,会被驳回;因此我们用的三方框架都是静态库,开发过程中也只是自定义静态库,达到共享代码的目的。
framework库生成
1.创建framework工程
Xcode->File->New->Project创建工程
![](https://img.haomeiwen.com/i7866378/2e4872e8eca97a62.png)
![](https://img.haomeiwen.com/i7866378/fbc1049b7afd8938.png)
2.framework工程设置
动态库静态库设置:MyFramework->targets->Build Settings->Mach_O Type设置:默认值为Dynamic Library动态库,Static Library静态库
![](https://img.haomeiwen.com/i7866378/4b8ced87e2f00642.png)
兼容设备设置:设置值详情见iOS中关于库的基本知识中的iOS设备分类
![](https://img.haomeiwen.com/i7866378/99f8b6f2e45af807.png)
开发环境模式设置:设置值详情见iOS中关于库的基本知识中的根据开发环境的不同可以分为四种文件版本
![](https://img.haomeiwen.com/i7866378/103e5f25f4f8047b.png)
![](https://img.haomeiwen.com/i7866378/3a4c77cf88141073.png)
3.framework工程代码编写
自定义工程代码:
+ (void)logMethodName {
NSLog(@"TextObject_logFunctionName");
}
+ (UIImage *)getLibraryBundleImage {
// 获取Resource.bundle文件夹路径
NSString * rescourcePath = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];
// 获取Resource.bundle文件夹bundle对象
NSBundle * bundle =[NSBundle bundleWithPath:rescourcePath];
// 获取Resource.bundle文件夹中调用图片的路径
NSString * imagePath = [bundle pathForResource:@"null_page" ofType:@"png"];
// 获取图片
UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
return image;
}
![](https://img.haomeiwen.com/i7866378/3acc20e34539443c.png)
4.设置framework工程对外可访问代码文件
![](https://img.haomeiwen.com/i7866378/4cc26730c8336032.png)
5.新建设置framework工程Bundle资源文件
![](https://img.haomeiwen.com/i7866378/0b53ccb27359b287.png)
![](https://img.haomeiwen.com/i7866378/c111e49723b053a1.png)
设置Bundle使用的平台模式,这里使用iOS平台。
![](https://img.haomeiwen.com/i7866378/1867aaf2c5303aa9.png)
Bundle资源文件夹参数COMBINE_HIDPI_IMAGES默认为YES,即图片是tiff格式;修改该参数为NO。
![](https://img.haomeiwen.com/i7866378/7437660f680fe34e.png)
framework库的生成和合并
如下图右侧箭头为选择编译环境,选择上面箭头为发布环境,下面的箭头为调试环境;选择左侧上面箭头为生成库文件,下面箭头为生成Bundle资源文件;选择后command+b编译成功,生成对应的文件。
![](https://img.haomeiwen.com/i7866378/a148584e44012052.png)
![](https://img.haomeiwen.com/i7866378/7cf14a83ca336d20.png)
framework调试和发布环境文件的两种合并,其实是Debug-iphonesimulator文件夹和Debug-iphoneos文件夹下的MyFramework可执行的文件的合并。
![](https://img.haomeiwen.com/i7866378/a4b34d1f0054996a.png)
用终端命令合成:-lipo -create 文件名1 文件名2 -output 合成的文件名
用脚本合成:
脚本代码:
# Sets the target folders and the final framework product.
FMK_NAME=${PROJECT_NAME}
# 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=${SRCROOT}/Products/${FMK_NAME}.framework
# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos clean build
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build
# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
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 "${INSTALL_DIR}/${FMK_NAME}"
rm -r "${WRK_DIR}"
open "${INSTALL_DIR}"
![](https://img.haomeiwen.com/i7866378/8b4f9f8b1ec6fdd2.png)
framework的使用
1.导入framework库和bundle文件
![](https://img.haomeiwen.com/i7866378/a47e5860b0869272.png)
2.设置导入framework库的工程
详情见iOS中关于库的基本知识的“Other Linker Flags标记值的说明”。
![](https://img.haomeiwen.com/i7866378/37f869996cabd02e.png)
3.运行工程
静态库运行成功,但是动态库运行出错。动态库编译时和静态库是不同的,详情见iOS中关于库的基本知识的“动态库和静态库使用中的不同”。
![](https://img.haomeiwen.com/i7866378/48ae8253071ee80f.png)
解决方法。
![](https://img.haomeiwen.com/i7866378/aefc1a1cc6f4c5e0.png)
4.调用代码运行
静态库调用代码成功。
![](https://img.haomeiwen.com/i7866378/5648b5a308509134.png)
动态库调用代码会出现警告“missing submodule”,解决方法是在动态库的同名.h文件中加入#import "警告的文件名",重新运行警告消失;在生成动态库时,应将供外界访问的文件在动态库的同名.h文件进行声明。
![](https://img.haomeiwen.com/i7866378/a243b567449d897f.png)
![](https://img.haomeiwen.com/i7866378/c685ce7ab9cad530.png)