一般的Swift項目(Executable)中如需使用Objc庫可以使用Bridging Header的方式,然而如果Swift項目是Framework則無法采用此種方式,這時就需要另一種方式:Module Map。
下面以CommonCrypto為例
- 在我們的Swift Framework工程中添加一個新的Target, 類型選擇Framework,起名為CommonCrypto
- 給CommonCrypto添加一個文件,類型選擇Other->Configuration Settings File,起名為CommonCrypto.xcconfig,這是一個配置文件,其內容為:
MODULEMAP_FILE[sdk=iphoneos] = $(SRCROOT)/CommonCrypto/iphoneos.modulemap
MODULEMAP_FILE[sdk=iphonesimulator] = $(SRCROOT)/CommonCrypto/iphonesimulator.modulemap
分別對應真機和模擬器上的modulemap文件。 - 創建配置文件中引用的modulemap模塊文件:
- iphoneos.modulemap 內容為:
module CommonCrypto [system] {
header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"
export *
} - iphonesimulator.modulemap 內容為:
module CommonCrypto [system] {
header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/CommonCrypto/CommonCrypto.h"
export *
}
如果兩個文件內容相同可以使用同一文件,header字段為framework的頭文件路徑。
- iphoneos.modulemap 內容為:
-
選擇我們的Framework Project設置Info->Configurations,找到Target CommonCrypto,將其Debug和Release的Based On Configuration File設置為CommonCrypto,即對應CommonCrypto.xcconfig文件。
未命名.jpg -
選擇我們的Framework Target設置 Build Phases 添加依賴CommonCrypto
屏幕快照 2017-03-30 下午9.49.28.png - 大功告成