一、umbrella framework
- 將多個已經封裝好的 framework 封裝成一個,封裝的這種 framework 就是 umbrella framework,
- Apple 的官方檔案中明確提到了不建議自己去創建 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
- 創建并使用 UmbrellaFramework 分為三部分:
-
- SubFramework:創建一個基礎 framework;
-
- UmbrellaFramework:framework 里封裝 framework;
-
- UmbrellaFrameworkDemo:使用 demo,
二、創建一個基礎的 framework
- 創建一個 framework 工程:Subframework;

- 添加 SubSayHello 類,添加 sayHello 方法;
@interface SubSayHello : NSObject
- (void)sayHello;
@end
@implementation SubSayHello
- (void)sayHello {
NSLog(@"say Hello");
}
@end
- 在 SubFramework.h 頭檔案中匯入 SubSayHello.h:
#import <Subframework/SubSayHello.h>
- 將 SubSayHello.h 添加到 Target -> Build Phases -> Headers -> Public,可手動拖拽;
- Build Settings -> Mach-O Type 選擇 Static Library 靜態庫;
- 生成通用 framework:
-
- 方式一:分別在模擬器和真機下編譯工程,生成兩個 framework,用命令列合并成一個通用的(注意:如果執行命令報錯,可以將結果地址改為 /Users/yydw/Desktop/Subframework.xx,生成后再將后綴名去掉):
$ lipo -create [真機 Framework 二進制檔案路徑] [模擬器 Framework 二進制檔案路徑] -output [結果路徑]

$ lipo -create /Users/ydw/Library/Developer/Xcode/DerivedData/Subframework-hkwchwbjmtuhoseinwkzbtcjxpbj/Build/Products/Debug-iphoneos/Subframework.framework/Subframework /Users/ydw/Library/Developer/Xcode/DerivedData/Subframework-hkwchwbjmtuhoseinwkzbtcjxpbj/Build/Products/Debug-iphonesimulator/Subframework.framework/Subframework -output /Users/ydw/Desktop/Subframework
-
- 方式二:腳本生成
-
-
- 為 SubFramework 工程添加 Target -> Aggregate:
-

-
-
- 在新添加的 Target 中添加腳本:
-

-
-
- 腳本內容:
-
# Sets the target folders and the final framework product.
FRAMEWORK_NAME=LibraryName
FRAMEWORK_VERSION=1.0
FRAMEWORK_CONFIG=Release
# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_PATH=${PROJECT_DIR}/Products/
INSTALL_DIR=${INSTALL_PATH}/${FRAMEWORK_NAME}.framework
# Working dir will be deleted after the framework creation.
WORK_DIR=build
DEVICE_DIR=${WORK_DIR}/${FRAMEWORK_CONFIG}-iphoneos/${FRAMEWORK_NAME}.framework
SIMULATOR_DIR=${WORK_DIR}/${FRAMEWORK_CONFIG}-iphonesimulator/${FRAMEWORK_NAME}.framework
xcodebuild -configuration "${FRAMEWORK_CONFIG}" -target "${FRAMEWORK_NAME}" -sdk iphoneos
echo "Build simulator"
xcodebuild -configuration "${FRAMEWORK_CONFIG}" -target "${FRAMEWORK_NAME}" -sdk iphonesimulator
# Creates install directory if it not exits.
if [ ! -d "${INSTALL_DIR}" ]
then
mkdir -p "${INSTALL_DIR}"
fi
# Creates headers directory if it not exits.
if [ ! -d "${INSTALL_DIR}/Headers" ]
then
mkdir -p "${INSTALL_DIR}/Headers"
fi
# Remove all files in the headers diectory.
for file in `ls "${INSTALL_DIR}/Headers"`
do
rm "${INSTALL_DIR}/Headers/${file}"
done
# Remove binary library file.
rm -f ${INSTALL_DIR}/${FRAMEWORK_NAME}
# Copies the headers files to the final product folder.
if [ -d "${DEVICE_DIR}/Headers" ]
then
for file in `ls "${DEVICE_DIR}/Headers"`
do
cp "${DEVICE_DIR}/Headers/${file}" "${INSTALL_DIR}/Headers/${file}"
done
fi
# copy nibs to bundle,then copy bundle to final folder
BUNDLE_DIR=${DEVICE_DIR}/${FRAMEWORK_NAME}.bundle
if [ -d "${BUNDLE_DIR}" ];then
if ls ${DEVICE_DIR}/*.nib >/dev/null 2>&1;then
rm -rf ${BUNDLE_DIR}/*.nib
cp -rf ${DEVICE_DIR}/*.nib ${BUNDLE_DIR}
fi
rm -rf "${INSTALL_DIR}/${FRAMEWORK_NAME}.bundle"
cp -R "${BUNDLE_DIR}" "${INSTALL_DIR}/${FRAMEWORK_NAME}.bundle"
fi
echo "Merge with simulator"
# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FRAMEWORK_NAME}" "${SIMULATOR_DIR}/${FRAMEWORK_NAME}" -output "${INSTALL_DIR}/${FRAMEWORK_NAME}"
open "${INSTALL_PATH}"
# rm -r "${WORK_DIR}"
- 查看 framework 支持的架構:
$ lipo -info [framework 二進制檔案路徑]
三、framework 里封裝 framework
- 創建 Cocoa Touch Framework 工程 UmbrellaFramework;
- 匯入 SubFramework:

- 選擇 Target -> Build Phases -> 點擊左上角+號 -> New Copy Files Phase 添加 Copy Files,將 SubFramework 添加到 Copy Files,選擇 Destination 為 Frameworks:

- 添加 UmbrellaSayHello 類,添加 sayHello 方法,并在 sayHello 方法中呼叫 SubFramework 的 sayHello 方法:
@interface UmbrellaSayHello : NSObject
- (void)sayHello;
@end
#import <Subframework/SubSayHello.h>
@implementation UmbrellaSayHello
- (void)sayHello {
NSLog(@"%s", __func__);
SubSayHello *ssh = [[SubSayHello alloc] init];
[ssh test];
}
@end
- UmbrellaFramework.h 頭檔案中匯入將 UmbrellaSayHello.h:
#import <UmbrellaFramework/UmbrellaSayHello.h>
- 將 UmbrellaSayHello.h 添加到 UmbrellaFramework 的公共 headers 中;
- Architectures 添加 armv7s;
- 連接選項 Mach-O Type 不用改,選擇默認選項 Dynamic Library,這意味著外層的 UmbrellaFramework 是一個動態庫,
- 生成真機和模擬器都能用的 framework,詳情請參考:iOS之深入決議構建動態庫與framework動態更新,
四、使用 UmbrellaFramework
- 創建工程 UmbrellaFrameworkDemo;
- 嵌入UmbrellaFramework,選擇工程 -> General -> Embedded binaries,添加UmbrellaFramework,UmbrellaFramework 將會同時添加到 Linked Frameworks and Libraries:

- 工程中使用:
#import <UmbrellaFramework/UmbrellaFramework.h>
- (void)viewDidLoad {
[super viewDidLoad];
UmbrellaSayHello *ush = [[UmbrellaSayHello alloc] init];
[ush sayHello];
}
五、UmbrellaHeader
① 什么是 umbrella header?
- framework 的檔案明明被主工程參考了,但是在編譯的時候依舊拋出下面的警告:
Lexical or Preprocessor Issue - Umbrella header for module 'xxx' does not include header 'xxx.h'
- 參考官方檔案《Introduction to Framework Programming Guide》,可以了解到 Framework 區分Standard Framework 和 Umbrella Framework,但是并沒有找到官方檔案有對 Umberlla framework 給出明確的定義,在官方檔案《Anatomy of Framework Bundles》章節中, 找到三段比較合理說明 Umbrella Framework 的話:
Umbrella frameworks add minor refinements to the standard framework structure, such as the ability to encompass other frameworks
The structure of an umbrella framework is similar to that of a standard framework, and applications do not distinguish between umbrella frameworks and standard frameworks when linking to them. However, two factors distinguish umbrella frameworks from other frameworks. The first is the manner in which they include header files. The second is the fact that they encapsulate subframeworks.
Physically, umbrella frameworks have a similar structure to standard frameworks. One significant difference is the addition of a Frameworks directory to contain the subframeworks that make up the umbrella framework.
- 字面上的意思應該是在標準的 Framework 做了一些改良的作業,使其可以嵌套包含 Framework,在物理結構上,Umbrella Framework 只在包含頭檔案的方式以及是否包含子 Framework 和普通的 Framework 存在區別,
- 那么參考頭檔案的地方又有什么區別呢? 還是參考官方檔案參考:
For most frameworks, you can include header files other than the master header file. You can include any specific header file you want as long as it is available in the framework’s Headers directory. However, if you are including an umbrella framework, you must include the master header file. Umbrella frameworks do not allow you to include the headers of their constituent subframeworks directly. See Restrictions on Subframework Linking for more information.
- 簡單翻譯一下: 普通的 Framework 可以通過參考對應的 heaedr 檔案而不是 Master Header File 去參考需要使用的類,只需要對應的 header 頭檔案在 Headers 檔案夾下暴露,并沒有強制要求參考 Master Header File,Umbrella Framework 要求必須要參考 Master Header File,并且頭檔案中不能直接參考子 Framework 的東西,上述描述已經說了 Umbrella Framework 一定要參考 Master Header File,而 Umbrella Framework 的 Master Header File 就是 Umbrella header 檔案,
- 官方說明中只有強制規定一定要參考 Umbrella Header 檔案,但是卻沒有說能不能單獨參考 Umbrella Framework 的其他頭檔案呢? 我們可以自己試驗一下:
-
- 在 Umbrella Framework 新建一個 testObject 類,分別產生了 testObject.h 和 testObject.m 檔案,
-
- 打開 Framework 組態檔,在 Build Phases 的 Headers 里的 Public 目錄下,將 testObject.h 檔案添加進去,
-
- Build Framework 看是否報錯,
-
- 在主工程中呼叫初始化 testObject 物件,看編譯是否報錯,
- 執行結果:
-
- 上面的第三步沒有編譯報錯, 但是報出了 Lexical or Preprocessor Issue - Umbrella header for module ‘STDemoUI’ does not include header ‘testObject.h’ 的警告,
-
- 上面的第四步執行正常,
- 總結一下:
-
- Standard Framework 不能包含 Sub Framework;Umbrella Framework 可以包含子 Framework;
-
- Standard Framework 可以直接參考需要使用的頭,也可以通過參考 Master Header file 來參考需要使用的類;Umbrella Framework 需要通過參考 Master Header File(Umbrella Header) 來參考需要使用的類,
② 規范的寫法
- Umbrella Framework 默認會創建一個同名 .h 檔案做為 Umbrella Header 檔案,規范的寫法當然是遵從默認的模式,將所有需要暴露的頭檔案都寫在 Umbrella Header 檔案中,
- 例如:YDWDemoUI.framework 工程包含了 YDWClassOne、YDWClassTwo 和 YDWClassThree 三個類,YDWDemoUI 會生成一個默認的傘頭檔案(直譯 Umbrella Header)YDWDemoUI.h,假設該 framework 的三個類均需要在外部呼叫使用,則 YDWDemoUI.h 需要將三個類的參考均寫入傘頭檔案中,
// YDWDemoUI.h
// ...
#import <YDWemoUI/YDWClassOne.h>
#import <YDWemoUI/YDWClassTwo.h>
#import <YDWemoUI/YDWClassThree.h>
- 在需要呼叫的主工程中, 僅僅只要將 Umbrella Header 參考即可呼叫所有在 Umbrella Header 中包含的類:
// 在主工程需要應用的類中包含Umbrella Header
#import <YDWDemoUI/YDWDemoUI.h>
③ 重命名 umbrella header
- 如果大家都遵從默認的 Umbrella Framework 的寫法,在同名頭檔案中寫需要暴露的參考頭檔案,那么就不需要考慮怎么重命名 Umbrella header 了,
- 很多時候,理想和現實是有差距的,程式員寫代碼多數是在二次接手進行開發的,假設公司的前輩已經將 Framework 的同名檔案用作了一個邏輯類, 給同名檔案創建了 .m 檔案, 并已經書寫了邏輯并應用了各個工程里面去了,那么顯然遷移頭檔案功能代碼是不可能的,因為很多依賴該 Framework 的業務部門都需要針對庫進行代碼優化,
- 在這種不能將同名檔案作為 Umbrella header 的情況下,我們又不想通過 Public 強制暴露頭檔案的情況下(不寫在 Umbrella Header 中會有警告),就需要對 Umbrella Header 進行指定,
- 指定 Umbrella Header 入口在哪里呢?
-
- 在工程全域搜索 umbrella 關鍵字 - Failed;
-
- 在 Build Settings 里搜索umbrella關鍵字 - Failed;
-
- 在打包好的 YDWDemoUI.framework 中搜索 umbrella 關鍵字 - Success;
- 雙擊點開 YDWDemo.framework,內容如下:

- 初略看名稱可以推測出每個檔案以及檔案夾所承擔的作用:
-
- _CodeSignature:保存簽名相關檔案
-
- Headers:framework 暴露的所有頭檔案
-
- Info.plist:描述了該 framework 所包含的專案配置資訊
-
- UmbrellaFramework:編譯后的核心庫檔案
-
- Modules:模塊相關檔案夾,目測只包含了 module.modulemap 檔案
-
- Frameworks:包含的子 framework
- 在 module.modulemap 檔案中找到了 umbrella 關鍵字,檔案內容如下:
framework module YDWDemoUI {
umbrella header "YDWDemoUI.h"
export *
module * { export * }
}
- 原來 Framework 的 umbrella header 是在這個位置被指定的,但是這個已經是編譯好的工程, 總不能每次編譯好了再進到包里面修改下,既然已經找到 umbrella header 是在 module 中去指定,那么就用 module 作為關鍵字再去 Build Settings 里重新搜索下,
- 這回在 Kernel Module 和 Packaging 中均找到了 Module 關鍵字,在 Packaging 標簽中,有一項 Module Map File 屬性,看名字應該是用來指定 modulemap 檔案的,

- 指定 Modulemap 檔案
-
- 創建一個新的 .h 檔案,如:YDWHeader.h,將所有需要暴露的頭檔案均寫入 YDWHeader.h;
-
- 創建一個新的 modulemap 檔案,如:stdemoalt.modulemap;
-
- 在新的 modulemap 中指定 umbrella header,
framework module YDWDemoUI {
umbrella header "YDWHeader.h"
export *
module * { export * }
}
-
- 在 framework 的 Build Settings 中的 Module Map File 指定新建的 modulemap 檔案;
-
- CMD+B 編譯,打開 framework 包中的 Module 檔案夾,看是否包含了新指定的 modulemap,
④ 總結
- 簡單地梳理了官方文章關于 Umbrella Framework 和 Umbrella Header 的介紹說明,產生警告的原因是沒有參考 umbrella header 或者暴露頭沒有寫在 umbrella header 中,在 umbrella header 被已使用的前提下,本文提供了一種通過重命名 Umbrella Header 檔案的方式來消除警告的解決方案,
- 雖然參考警告可以被消除,但是建議大家還是采用規范的做法:盡量不要在同名頭檔案中寫業務邏輯代碼, 用同名檔案作為 Umbrella 庫的 Master Header File,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/297167.html
標籤:其他
