1. 程式人生 > >生成 iOS 動態庫與靜態庫方法

生成 iOS 動態庫與靜態庫方法

支援原創,更多內容歡迎訪問部落格:

在程式開發過程中,免不了需要對程式進行封裝:比如給第三方使用者來呼叫的SDK,或者給其他開發人員來使用,同時他們又無需或者無權瞭解其中細節的時候,就需要用到動態庫封裝。 一、建立FrameWork工程 新建工程,選擇建立FrameWork工程或者靜態庫(Static Library)。
二、選擇匯出的標頭檔案和資原始檔 將需要封裝的程式碼直接拖進。這時,Xcode便會自動將需要匯出的標頭檔案和資原始檔方放好,你自己也可以根據需要來進行調整。
三、生成FrameWork 直接Run該工程,便可建立FrameWork動態庫。 但是,需要注意的是,如果你是執行在模擬器上,則產生的動態庫的編譯版本只能是支援 i386 和 x86_64 版本。

如果是執行在真機上,則只能產生 arm64 armv7 armv7s 三種架構。
這兩者需要分別單獨提供給其他開發人員來使用,如果需要生成一個通用版的動態庫,則需要進行合併。 lipo -create libCryptSecurity.framework/libCryptSecurity libCryptSecurity1.framework/libCryptSecurity -output ~/Desktop/libt 但是需要注意,如果你要控制安裝包的大小的話,合併之後的庫檔案大於合併之前兩者的總和。
四、簡化合並操作 如果每次庫檔案有改動,都需要重新打包併合並,想來是一件非常繁瑣的工作,這時候可以用一個 Aggregate 的Target 來進行合併: 開啟 File->New->Target,選擇 Aggregate 。

在生成的程式碼裡面貼上如下的 script 指令碼。
程式碼:
# 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}"

選中新建的Target->Run。這時候 Build 的FrameWork 就會自動彈出來