開發(fā)過程中會遇到各種各樣的情況,比如需要將幾個已經(jīng)封裝好的framework封裝成一個磁椒,統(tǒng)一提供出去堤瘤,封裝的這種framework就是umbrellaframework。Apple的官方文檔中明確提到了不建議自己去創(chuàng)建umbrellaframework浆熔,但是既然遇到了這種情況本辐,還是動手試一試。
首先先引入Apple的 Guidelins for Creating Frameworks 的一段
Don’t Create Umbrella Frameworks
While it is possible to create umbrella frameworks using Xcode, doing so is unnecessary for most developers and is not recommended. Apple uses umbrella frameworks to mask some of the interdependencies between libraries in the operating system. In nearly all cases, you should be able to include your code in a single, standard framework bundle. Alternatively, if your code was sufficiently modular, you could create multiple frameworks, but in that case, the dependencies between modules would be minimal or nonexistent and should not warrant the creation of an umbrella for them
文章將分三個部分逐步創(chuàng)建并使用UmbrellaFramework
- SubFramework 創(chuàng)建一個基礎(chǔ)framework
- UmbrellaFramework framework里封裝framework
- UmbrellaFrameworkDemo 使用demo
本篇是第一部分医增,這一部分將會一步步創(chuàng)建一個基礎(chǔ)framework慎皱,那么伸伸懶腰,我們開始吧叶骨!
demo地址:https://github.com/huibaoer/Demo_UmbrellaFramework
創(chuàng)建一個基礎(chǔ)的framework
1.創(chuàng)建一個framework工程:Subframework
2.添加類SubSayHello宝冕,添加sayHello方法
3.在SubFramework.h頭文件中導(dǎo)入SubSayHello.h
4.將SubSayHello.h添加到公共Header中
5.Architectures添加armv7s
6.連接選項 Mach-O Type 選擇Static Library靜態(tài)庫
7.生成通用framework
到了這一步framework已經(jīng)基本完成了,只需要生成最終需要使用的framework了邓萨。一般有兩種方式生成:
- 分別在模擬器和真機下運行工程地梨,分別導(dǎo)出兩個運行出來的framework,用命令行合并成一個通用的framework
- 在工程中添加腳本缔恳,運行腳本生成通用framework
兩種方式介紹的文章很多宝剖,這里不做過多解釋,下面使用第二種添加腳本的方式生成framework
7.1為SubFramework工程添加Target -> Aggregate
7.2在SubFramework新添加的Target中添加腳本
腳本內(nèi)容如下:
# 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}"
7.3運行新添加的Target万细,會自動彈出窗口,包含了已經(jīng)生成好的framework纸泄。大功告成赖钞!
8.最后,可以檢查一下生成的framework信息聘裁,命令行執(zhí)行如下命令雪营,注意cd到SubFramework的所在目錄
lipo -info SubFramework.framework/SubFramework
正常情況下結(jié)果如下,支持的架構(gòu)已經(jīng)顯示出來
Architectures in the fat file: SubFramework.framework/SubFramework are: armv7 armv7s i386 x86_64 arm64
當(dāng)然衡便,也可以自己建一個demo工程献起,將SubFramework導(dǎo)入洋访,試著調(diào)用一下 SubSayHello 的 sayHello 方法。相信會愉快的收到響應(yīng)的谴餐,?