在網(wǎng)上搜索了很多關(guān)于靜態(tài)庫(kù)的知識(shí),最后都是不了了之,遂怒發(fā)沖冠,暴走了一次,將Framework打包的一些詳細(xì)細(xì)節(jié)加以記錄,給予有同樣需求的朋友一些幫助。
首先,F(xiàn)ramework作為靜態(tài)庫(kù),是蘋(píng)果公司允許的一種打包機(jī)制,通常SDK的制作也是通過(guò)講.h文件和.m文件加以編譯,打包成framework,而例如圖片,xib,storyboard,xcassets等資源文件通常是以bundle的方式進(jìn)行打包,framewrok和bundle本質(zhì)上都是一種資源包,以一種包集合的概念來(lái)對(duì)目標(biāo)文件進(jìn)行封裝。同時(shí),Cocoapods的出現(xiàn)為我們帶來(lái)了更加便捷的方便,搜索了很多也找不到一個(gè)很滿(mǎn)意的可以支持Pods方式的靜態(tài)庫(kù)打包,我們接下來(lái)就嘗試進(jìn)行依賴(lài)Cocoapods方式的Framework方式的打包。
1. Framework
Framework打包其實(shí)很容易,首先,我們將原有工程的項(xiàng)目進(jìn)行剝離,這里我用了SDAutoLayout的Demo進(jìn)行操作。
附上傳送門(mén):SDAutoLayoutDemo
- 新建Framework工程


配置工程
1.IOS版本配置
根據(jù)需要 7.0&&8.0均可 主要是兼容問(wèn)題
2.mach-O type
由于是靜態(tài)庫(kù) 需要設(shè)置setting中Mach-O type 為Static Library-
終端導(dǎo)入Cocoapods
導(dǎo)入項(xiàng)目所需要的Pods
framework_8.jpg -
添加打包文件
1.將項(xiàng)目需要打包的文件添加到項(xiàng)目文件目錄中
2.將項(xiàng)目所依賴(lài)的庫(kù)文件(靜態(tài)庫(kù),framework,lib導(dǎo)入并link)
3.創(chuàng)建項(xiàng)目頭文件,需要包含全部打包的文件
格式如:#import<demo1/demo1.h>
4.將Build Phases中headers頭文件進(jìn)行處理
framework_14.jpg 進(jìn)行編譯
由于項(xiàng)目屬于Framework,編譯后只能在模擬器或真機(jī)中運(yùn)行,而實(shí)際需求是要在兩者內(nèi)都可以正常操作,在編譯前,我們這里要借助mac系統(tǒng)腳本文件Aggregate進(jìn)行合并打包。
1.創(chuàng)建aggregate target
2.添加腳本
# Sets the target folders and the final framework product.
# 如果工程名稱(chēng)和Framework的Target名稱(chēng)不一樣的話(huà),要自定義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 -arch armv7 -arch armv7s -arch arm64 clean build
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator -arch x86_64 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 "${SRCROOT}/Products/"
3.設(shè)置Run 模式為 release模式

4.刪除Frameworks中的libPod.a文件
這個(gè)需要強(qiáng)調(diào)一下,必須要?jiǎng)h除這個(gè)文件,因?yàn)榇虬笤擁?xiàng)目不存在拿給其他項(xiàng)目用的時(shí)候,該文件會(huì)重復(fù),引起報(bào)錯(cuò)。

5.編譯生成Framework

2. Bundle打包
前文說(shuō)過(guò),例如圖片,xib,storyboard,xcassets等資源文件通常是以bundle的方式進(jìn)行打包,framewrok和bundle本質(zhì)上都是一種資源包,以一種包集合的概念來(lái)對(duì)目標(biāo)文件進(jìn)行封裝。且bundle在xcode7中已經(jīng)被劃分到mac項(xiàng)目文件中來(lái),新建時(shí)要從OS X文件中尋找。
-
新建Bundle
bundle_1.jpg -
配置Bundle
由于是OS X系統(tǒng)下的Bundle, 新建后我們要對(duì)其進(jìn)行一些設(shè)置 - "Base SDK" 設(shè)置為 "IOS 8.3" (Xcode 6.3.2為例)
- "Build Active Architecture Only" 設(shè)置為 "YES"
- "Debug Information Format" 設(shè)置為 "DWARF with dSYM File"
- "OS X Deployment Target" 設(shè)置為 "Compiler Default"
- "Skip Install" 設(shè)置為 "NO"
- "Strip Debug Symbols During Copy" 中"Release"模式設(shè)置為 "YES"
- "IOS Deployment Target" 設(shè)置為 "IOS 7.0"
- "COMBINE_HIDPI_IMAGES" 設(shè)置為 "NO"

-
導(dǎo)入打包資源文件并編譯打包
這里測(cè)試放入png,xib,storyborad,xcassets文件,以此類(lèi)推.

3. 主項(xiàng)目調(diào)用
需要將主項(xiàng)目Enable Bitcode 設(shè)置為“YES”


需要注意的是 如果主項(xiàng)目中采用了Category的話(huà) 需要在引用framework的工程中設(shè)置 Build Pharse -> Other Linker Flags中加上 -all_load 就OK了,再次運(yùn)行工程,可以完成所有的打印過(guò)程.
4. 調(diào)用方法
- bundle路徑(定義宏)
#define MYBUNDLE_NAME_2 @"bundle1.bundle"
#define MYBUNDLE_PATH_2 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME_2]
#define MYBUNDLE_2 [NSBundle bundleWithPath: MYBUNDLE_PATH_2]
- 加載storyboard
//加載storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SFMapKit" bundle:MYBUNDLE_2];
UIViewController *test = [storyboard instantiateViewControllerWithIdentifier:@"test"];
[self.navigationController pushViewController:test animated:YES];
// bundle加載圖片
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageWithContentsOfFile:[MYBUNDLE_PATH_2 stringByAppendingPathComponent:@"test.png"]];
[self.view addSubview:imageView];
- xcassets加載圖片
// xcassets加載圖片
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 260 , 40, 40)];
UIImage *image1 = [UIImage imageNamed:@"ScanBook_HL" inBundle:MYBUNDLE_2 compatibleWithTraitCollection:nil];
imageView1.image = image1;
[self.view addSubview:imageView1];
- 加載xib
//加載xib
-(instancetype)init{
NSBundle *bundle = [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bundle1.bundle"]];
self = [super initWithNibName:@"testxib" bundle:bundle];
return self;
}
好了到此為止,問(wèn)題解決!


