使用Cocoapods進(jìn)行組件化開發(fā)遇到的坑

這里先給出簡(jiǎn)單使用私有庫(kù)的教程鏈接:
CocoaPods三重奏(二) 之 組件化開發(fā)

Specs satisfying the XXX dependency were found, but they required a higher minimum deployment target

原因是“XXX”需要需要一個(gè)更高的最低部署目標(biāo)(就是podspec文件中依賴的庫(kù)的最低部署版本)
把工程部署版本改為 依賴庫(kù)的最低版本

include of non-modular header inside framework module

背景:
當(dāng)制作自己的pod時(shí),我的代碼依賴 MBProgressHUD第三方庫(kù),pod spec lint驗(yàn)證過程一直出這個(gè)錯(cuò)誤
解決辦法:
1.buldsetting 中設(shè)置 Allow Non-modular Includes In Framework Modules 為 YES
但是對(duì)我無用
2.將#import "**.h" 第三方庫(kù)寫在 .m文件中,而不是放在.h文件中即可
e.g.:
我的文件 UIView+Hint
將 #import "MBProgressHUD.h" 這行代碼放在 UIView+Hint.m文件中

pod lib lint 和 pod spec lint 命令的區(qū)別

pod lib lint是只從本地驗(yàn)證你的pod能否通過驗(yàn)證
pod spec lint是從本地和遠(yuǎn)程驗(yàn)證你的pod能否通過驗(yàn)證
我一般都是直接使用pod spec lint去驗(yàn)證pod有沒有問題

私有pod的驗(yàn)證

使用pod spec lint去驗(yàn)證私有庫(kù)能否通過驗(yàn)證時(shí)應(yīng)該,應(yīng)該要添加--sources選項(xiàng),不然會(huì)出現(xiàn)找不到repo的錯(cuò)誤
pod spec lint --sources='私有倉(cāng)庫(kù)repo地址,https://github.com/CocoaPods/Specs'
3.subspec
為了讓自己的Pod被導(dǎo)入時(shí)顯示出良好的文件層劃分,subspec是必須的。
若subspec要依賴其它的subspec,則subspec的dependency后面接的不是目錄路徑,而是specA/specB這種spec關(guān)系

私有庫(kù)引用私有庫(kù)的問題

在私有庫(kù)引用了私有庫(kù)的情況下,在驗(yàn)證和推送私有庫(kù)的情況下都要加上所有的資源地址,不然pod會(huì)默認(rèn)從官方repo查詢。

pod spec lint --sources='私有倉(cāng)庫(kù)repo地址,https://github.com/CocoaPods/Specs'
pod repo push 本地repo名 podspec名 --sources='私有倉(cāng)庫(kù)repo地址,https://github.com/CocoaPods/Specs'
5.引用自己或第三方的framework或.a文件時(shí)
在podsepc中應(yīng)該這樣寫:

s.ios.vendored_frameworks = "xxx//.framework"
s.ios.vendored_libraries = "xxx/
/.a”

便捷地開發(fā)本地私有庫(kù)

Cocoapods就提供了一個(gè)開發(fā)模式,其實(shí)操作起來也是非常簡(jiǎn)單的事情,就是將所謂的引用路徑修改成本地路徑即可。就是講Podfile中的pod '庫(kù)名', :path => '本地路徑'即可。這樣在通常的修改代碼中是不需要執(zhí)行pod update的,但是對(duì)于如果修改了目錄結(jié)構(gòu)(添加、刪除或者移動(dòng)文件文件)或者是修改了Podspec文件的配置的話,最好是運(yùn)行一下pod update的命令。普通修改代碼的情況下就不需要運(yùn)行pod update命令和打tag了。
pod 'iOS-Test', :path => '../iOS-Test’

私有庫(kù)中添加資源(圖片、音視頻等)

方法共有三種:

第一種

spec.resources = ["Images/.png", "Sounds/"]
但是這些資源會(huì)在打包的時(shí)候直接拷貝的app的Bundle中,這樣說不定會(huì)和其它資源產(chǎn)生命名沖突

第二種

spec.resource = "Resources/MYLibrary.bundle"
把資源都放在bundle中,然后打包時(shí)候這個(gè)bundle會(huì)直接拷貝進(jìn)app的mainBundle中。使用的時(shí)候在mainBundle中查找這個(gè)bundle然后再搜索具體資源

NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"JZShare" withExtension:@"bundle"];
NSBundle bundle = [NSBundle bundleWithURL:bundleURL];
UIImage img = [UIImage imageNamed:icon inBundle:bundle compatibleWithTraitCollection:nil];
第三種
spec.resource_bundles = {
'MyLibrary' => ['Resources/
.png'],
'OtherResources' => ['OtherResources/
.png']
}
這種方法利用 framework 的命名空間,有效防止了資源沖突。
使用方法是先拿到最外面的 bundle,然后再去找下面指定名字 的 bundle 對(duì)象,再搜索具體資源
NSBundle *bundle = [NSBundle bundleForClass:[MYSomeClass class]];
NSURL *bundleURL = [bundle URLForResource:@"MyLibrary" withExtension:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithURL: bundleURL];
UIImage *img = [UIImage imageNamed:icon inBundle:bundle compatibleWithTraitCollection:nil];

如果私有庫(kù)添加了靜態(tài)庫(kù)或者dependency用了靜態(tài)庫(kù)

那么執(zhí)行pod lib lint還有pod spec lint時(shí)候需要加上—user-libraries選項(xiàng)
否則會(huì)出現(xiàn)'The 'Pods' target has transitive dependencies錯(cuò)誤

如果私有庫(kù)只引用其他庫(kù)的subspec

只需要依賴想依賴的subspec,不用管主spec(因?yàn)橐蕾噑ubspec必然要依賴主spec)

私有庫(kù)已經(jīng)通過驗(yàn)證并傳到私有repo也能通過pod search,但是就是pod install失敗。

這時(shí)候只要執(zhí)行pod update 然后去喝杯水就好了。。。(前提是你把官方源換成國(guó)內(nèi)的,不然從github上更新官方repo的速度你懂的。 更換官方源)

參考

what's the different between 'pod spec lint' and 'pod lib lint'
給Pod添加資源文件
Reject installation if a static library is used as a transitive dependency while using frameworks

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容