上回講到framework的創(chuàng)建和使用,這回我們來講講靜態(tài)庫和動(dòng)態(tài)庫。各位看官往下看。
1. 什么是靜態(tài)庫和動(dòng)態(tài)庫?
靜態(tài)庫:鏈接時(shí)完整地拷貝至執(zhí)行文件中,被多次使用就有多份冗余拷貝。
動(dòng)態(tài)庫:鏈接時(shí)不復(fù)制,程序運(yùn)行時(shí)由系統(tǒng)動(dòng)態(tài)加載到內(nèi)存,供程序調(diào)用,系統(tǒng)只加載一次,多個(gè)程序共用,節(jié)省內(nèi)存。
我們一開始創(chuàng)建的默認(rèn)framework就是動(dòng)態(tài)的,我們可以在Build settings->mach-O Type里修改static library來改成靜態(tài)庫。


我們改成static library后運(yùn)行,然后從products里把framework拷貝到之前那個(gè)sdk文件夾,因?yàn)槭庆o態(tài)庫,demo里配置改成Do Not Embed,運(yùn)行demo我們可以看到也能成功打印。

2. 腳本編寫
但是每次從products去拷貝framework有點(diǎn)麻煩,有沒有辦法可以自動(dòng)生成framework到我們項(xiàng)目指定的文件下底下呢?有,xcode里我們可以編寫腳本來實(shí)現(xiàn)。
1.首先我們先新建一個(gè)target。


2.然后添加一個(gè)run script phase

3.添加依賴

4.腳本編寫,我們先編寫個(gè)靜態(tài)庫的腳本

上一下代碼
# Sets the target folders and the final framework product.
FMK_NAME=TestFramework
# 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}/../sdk/framework/${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 "TestFramework" -sdk iphoneos clean
xcodebuild -configuration "Release" -target "TestFramework" -sdk iphonesimulator clean
xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "Release" -target "TestFramework" -sdk iphoneos build
xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "Release" -target "TestFramework" -sdk iphonesimulator 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}"
5.最后,我們運(yùn)行腳本target,可以看到在項(xiàng)目sdk文件夾下生成了個(gè)framework

6.動(dòng)態(tài)庫和靜態(tài)庫前面步驟都一樣,除了腳本不一樣,就直接上腳本代碼
# Sets the target folders and the final framework product.
FMK_NAME=TestFramework
# 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}/../sdk/${FMK_NAME}.framework
# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "TestFramework" -sdk iphoneos 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}" -output "${INSTALL_DIR}/${FMK_NAME}"
rm -r "${WRK_DIR}"