本文是基于Xcode9的開發環境搭建的,由于本人使用的.framework形式,所以只說.framework形式的靜態庫,其他可自行百度
提交appstore的應用使用的庫不允許包含i386或x86_64,否則會報錯
前提
庫:庫實際上是一種代碼共享的方式,主要用于代碼重用和源碼隱藏,通常分為動態庫和靜態庫。
靜態庫:鏈接時完整的拷貝至可執行文件中,被多次使用就有多份冗余拷貝。
動態庫:鏈接時不復制,程序運行時由系統動態加載到內存,供程序調用,系統只加載一次,多個程序共用,節省內存空間。
靜態庫和動態庫的存在形式
靜態庫:.a 和 .framework
動態庫:.dylib 和 .framework
支持多個設備需要了解Xcode指令集及配置,請跳轉
制作
一、創建工程
1、打開Xcode在導航欄選擇File -》New -》Project,選擇Cocoa Touch Framework,點擊Next創建工程
2、新建工程后目錄結構如下(TestApi是我自己添加的文件),在Targets -》Build Phases -》 Headers 選擇對外暴露的頭文件,Private為空則只暴露Public
3、配置Targets -》Build Settings
(1)使靜態庫支持bitcode,需要在Other C Flags添加-fembed-bitcode
(2)更改Mach-0 Type 為Static Library (默認創建的是Dynamic)
二、添加Aggregate Target
1、File -》New -》Target,選擇Cross-platform中的Aggregate并創建
2、選擇新加的Aggregate Target -》Build Phases 點擊“+”號選擇New Run Script Phase添加腳本
腳本內容:
# 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}"
三、制作通用靜態庫
方案一、Build的Target使用二中創建的即可,如下圖:
方案二、使用Carthage中的使你的framework支持Carthage章節