1.创建工程选择Cocoa Touch Framework

2 配置工程
2.1 IOS版本配置
根据需要兼容8.0或7.0即可
2.2 添加armv7s

2.3 在Linking中 Mach-O Type 设置为Static Library

2.4 添加Aggregate目的是为了生成的framework兼容模拟器和真机,也可以通过终端命令进行合并真机和模拟器的framework,用这种方法更方便

2.5 添加之后如下所示
可以看到Run Script

在shell中复制下面代码
# Sets the target folders and the final framework product.
# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME
# 例如: FMK_NAME = "MyFramework"
FMK_NAME="TCSearchReceiptSDK"
# 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}"
要完全一样,FMK_NAME="TCSearchReceiptSDK"这个地方需要改成自己的SDK名字。
3 选择你需要对外暴露的头文件,放在public中

4 创建好之后build选择Generic ios Device,为通用设备
编译出来的framework可以在真机和模拟器运行

5 直接build成功,会直接打开framework所在文件夹如下所示

FrameWork如何加载资源文件,需要使用bundle
例如图片,xib,storyboard,xcassets等资源文件通常是以bundle的方式进行打包,framewrok和bundle本质上都是一种资源包,以一种包集合的概念来对目标文件进行封装。且bundle在xcode7中已经被划分到mac项目文件中来,新建时要从OS X文件中寻找。
1.新建Bundle
image.png
2. 配置Bundle
baseSDK选择iOS

"Build Active Architecture Only" 设置为 "YES"


导入打包资源文件并编译打包
可以加入图片 xib 等各种所需资源
需要注意的是 如果主项目中采用了Category的话 需要在引用framework的工程中设置 Build Pharse -> Other Linker Flags中加上 -all_load 就OK了,再次运行工程,可以完成所有的打印过程.
如何获取bundle中的资源
创建一个TCBundleTool类,并提供几个获取bundle的方法
#import "TCBundleTool.h"
@implementation TCBundleTool
+ (NSBundle *)getBundle {
return [NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource:@"Bundle" ofType: @"bundle"]];
}
+ (NSString *)getBundlePath: (NSString *) bundleName{
NSBundle *myBundle = [TCBundleTool getBundle];
if (myBundle && bundleName) {
return [[myBundle resourcePath] stringByAppendingPathComponent: bundleName];
}
return nil;
}
+ (UIImage *)getBundleImage:(NSString *)name
{
static UIImage *bundleImage = nil;
if (bundleImage == nil) {
bundleImage = [[UIImage imageWithContentsOfFile:[[self getBundle] pathForResource:name ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
return bundleImage;
}
@end
加载xib文件
获取viewController的xib文件
self.view = [[[TCBundleTool getBundle] loadNibNamed:@"CHQueryDetailViewController" owner:self options:nil] lastObject];
获取图片
UIImage *image = [TCBundleTool getBundleImage:@"IOS_back@2x"];
第三方冲突的问题
有些必要的框架可能你SDK里面使用到了,项目工程里面也用到了,例如常用的AF,SD,MJ等,会有冲突。可以在编写SDK引入框架的时候不勾选Target,但是必须在项目中引入

网友评论