動(dòng)態(tài)庫(kù)形式:.dylib和.framework
靜態(tài)庫(kù)形式:.a和.framework
舉例demo:demoStatic
動(dòng)態(tài)庫(kù)和靜態(tài)庫(kù)的區(qū)別
1、動(dòng)態(tài)庫(kù)情況xib資源存在路徑
iOS在組件化過程中,一般來(lái)說(shuō),會(huì)把組件做成動(dòng)態(tài)庫(kù)的形式,也就是使用use_frameworks!
圖1
圖2
那么app運(yùn)行起來(lái)之后侧戴,當(dāng)app需要加載xib資源,會(huì)從
demoStatic.framework
這個(gè)動(dòng)態(tài)庫(kù)中尋找跌宛,如果xib資源放在了Assets文件夾下酗宋,那么系統(tǒng)應(yīng)該從demoStatic.bundle
里面找xib資源;如果xib資源放在了Classes文件夾下疆拘,那么系統(tǒng)應(yīng)該直接從demoStatic.framework
里面找(即demoStatic.bundle
同級(jí)目錄)蜕猫。下圖為
demoStatic_Example.app
的包內(nèi)容(靜態(tài)庫(kù)和動(dòng)態(tài)庫(kù)兩種情況,此時(shí)的xib資源都在Assets文件夾下入问,所以xib的資源都應(yīng)該在demoStatic.bundle
里面找丹锹,只是不同情況demoStatic.bundle
的位置不同)圖3|動(dòng)態(tài)庫(kù)|Assets
圖3|靜態(tài)庫(kù)|Assets
圖3|動(dòng)態(tài)庫(kù)|Classes
podspec
文件中聲明s.static_framework = true
,如下圖圖4
demoStatic
組件允許打包成靜態(tài)庫(kù)芬失,這樣的話楣黍,demoStatic_Example.app
包內(nèi)容的Frameworks路徑下就沒有demoStatic.framework
,所以這種情況下棱烂,必須把xib資源放到Assets文件夾下(參考圖3|靜態(tài)庫(kù))[因?yàn)殪o態(tài)庫(kù)的組件中租漂,它的bundle路徑在demoStatic_Example.app
下]。為了和圖片資源區(qū)分清楚颊糜,盡量在podSpec文件下指明路徑哩治,參考圖4的s.source_bundles
下面我寫了一個(gè)加載Assets文件夾下xib資源的方法
#import "NSBundle+Xib.h"
@implementation NSBundle (Xib)
///這是NSBundle的Category
/// 如果xib文件放到了Assets文件夾下使用這個(gè)方法獲取xib所在bundle(兼容靜態(tài)庫(kù)和動(dòng)態(tài)庫(kù))
/// @param bundleName bundle名稱
+ (instancetype)yl_xibWithBundle:(NSString *)bundleName {
if ([bundleName containsString:@".bundle"]) {
bundleName = [bundleName componentsSeparatedByString:@".bundle"].firstObject;
}
NSBundle *lastBundle = nil;
//沒使用framwork的情況下(靜態(tài)庫(kù))
NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
//使用framework形式(動(dòng)態(tài)庫(kù))
if (!associateBundleURL) {
associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
associateBundleURL = [associateBundleURL URLByAppendingPathComponent:bundleName];
associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
associateBundleURL = [associateBundleURL URLByAppendingPathComponent:bundleName];
associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"bundle"];
}
lastBundle = [NSBundle bundleWithURL:associateBundleURL];
return lastBundle;
}
@end