公司最近要开发一个SDK,在网上查了些资料,在踩了很多坑点之后,决定整理一下。
一、创建一个工作区
- 首先在桌面先创建一个DZMeBookRead文件夹(名字自己取)。
-
打开Xcode,创建一个工作区,路径放到文件夹MyTestSDK里面,Xcode的文件--- --- ---新的工作区,如下图所示。
创建工作区
之后可以看到DZMeBookRead文件中多了一个.xcworkspace文件
二、创建SDK
1.打开DZMeBookRead.xcworkspace文件,创建项目

选择Cocoa Touch Framework

2.把框架添加到工作空间里面

三、创建测试Demo
1.打开DZMeBookRead.xcworkspace文件,创建项目

2.选择Single View Application

3.添加应用到工作区

最终效果图

四、framework包的处理与项目配置
项目配置
1.在刚才创建的framework的项目中更改这几个参数设置

2.增加armv7s架构的支持

3.增加Bitcode支持
在Other Linker Flags 下添加一个-fembed-bitcode

framework包合并
这里只介绍使用脚本打包的方式,个人觉得比较好用。
1.创建一个Aggregare

2.嵌入脚本,选中刚刚创建的Aggregare,然后选中右侧的构建阶段,点击左边的+号,选择新的运行脚本阶段

3.下面把这段脚本复制进去(格式不要错,格式错编译不通过)
# Sets the target folders and the final framework product.
# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME
# 例如: FMK_NAME = "MyFramework"
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}"
最终效果如图所示

五、将framework和演示demo关联起来
按照下图中的五个步骤操作即可完成关联操作

然后在framework中修改代码就可以看到效果了
六、创建Bundle资源包
我们的项目中难免会用到图片资源和xib,storyboatd资源,我们可以将这些资源全部归类到bundle文件中,便于管理。
按照下图所示的顺序创建bundle资源包

配置Bundle资源包的构建参数
1.按照下图所示的方式删除安装目录Bundle文件不需要安装

2.设置COMBINE_HIDPI_IMAGES参数为NO,不然图片会被打包成.tiff后缀

3.设置Bitcode参数为NO,不然会报错。

3.修改bundle包的名字。默认是创建时的名字。(不改也可以)

4.设置bundle包与framework的关联

这样bundle的修改就能实时反映出来
5.建立bundle与测试demo的关联
bundle还要添加进测试demo中,这样才能生效
5.1找到bundle文件,如果找不到,请先把bundle编译一次

5.2将bundle包拖到测试demo的目录下面,注意文件的勾选。

6.使用bundle中的资源
将要使用的bundle集成到项目中后,就可以使用了。需要注意的就是,bundle是静态的,不进行编译的资源文件。所以,要使用bundle中的资源,就需要找到相应的资源路径。
这里废话就不多说了,贴代码!
- VC获得bundle中的资源
NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle" ofType :@ "bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name" bundle:resourceBundle];
- 图片获得bundle中的资源
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];
或者
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
NSString *imgPath= [bundlePath stringByAppendingPathComponent :@"img_collect_success.png"];
UIImage *image_1=[UIImage imageWithContentsOfFile:imgPath];
[imgView setImage:image_1];
至此,我们就完成了我们的项目配置,就可以安心的开发了。
最后附上本次demo的下载地址。
网友评论