iOS项目中SDK的开发与调试
公司最近要开发一个SDK,不知如何下手,请教了一个有SDK开发经验的同事,也在网上查了些资料,决定整理一下。整体上讲就是在SVN上创建一个项目路径,再用Xcode创建一个项目工程,把项目工程上传到SVN上就可以开发了,这里只讨论项目工程的创建。
一、创建一个workspace
1、在桌面上创建一个文件夹MyTestSDK(名字自己取)
2、打开Xcode,创建一个workspace,路径放到文件夹MyTestSDK里面,Xcode---File---New---Workspace

之后可以看到MyTestSDK文件夹里面多了一个.xcworkspace文件

二、创建SDK
1、打开MyTestSDK.xcworkspace文件,创建project

选择Cocoa Touch Framework

把framework添加到workspace里面

添加之后的效果图

三、创建Demo
1、打开MyTestSDK.xcworkspace文件,创建project

2、选择Single View Application

3、添加application到workspace

最终效果图

从上图可以看出,上面是demo,下面是sdk,怎么把这两者关联起来呢????????
四、Demo的关联以及SDK的开发调试
1、把SDK打包成framework
这个步骤网上有很多种教程,这里就取其中一个,其他方法有兴趣的同学可以自己研究
参考:qingmang.me/articles/5162103427194116731/
这里创建一个MyView类,继承于UIView,简单的重写了初始化方法,设置了颜色

设置Build Setting参数

更改参数,在Architectures下增加armv7s(iOS11上不支持armv7s,如果报错就不要添加),并选中。将Build Active Architecture only设置为NO

设置Headers,将要公开的头文件拖到Public下,要隐藏的放到Private或Project下,隐藏的头文件不能被引用

然后需要在MyTestSDK.h(必须是公开的,否则无法引用)中将所有要公开的.h引入

创建一个Aggregare


嵌入脚本,选中刚刚创建的Aggregare,然后选中右侧的Build Phases,点击左边的+号,选择New Run Script Phases

下面把这段脚本复制进去(格式不要错,格式错编译不通过)
# 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}"
如图所示:

最后编译,command + B,编译通过在finder中找到framework,拷贝出来

2、Demo与SDK的关联
把打包好的framework拖到demo里面

使用SDK,运行demo

运行的结果

这时候SDK跟Demo就关联起来了,此时修改下面SDK中的代码,运行demo就是修改后的效果,然后就可以愉快的在这个工程里面开发调试SDK了,如果我把MyView的颜色设置为yellow,再去运行demo,看一下效果

最后的最后,把这个工程上传到SVN就可以愉快的开发了。。。以上是我个人的理解,如有错误,请各位大牛批评指正,文中framework的打包方法参考网上资料,参考链接:qingmang.me/articles/5162103427194116731/