經過構建Cocoapods私有庫進行組件化開發探索

專題一

1、建立私有索引庫

   選Github或者碼雲均可以,本例以Github爲例。建立私有索引庫用來做爲本身組件庫的索引:git

2、本地添加私有索引庫

添加:pod repo add 索引庫名稱 索引庫地址github

例:pod repo add ZYHModule https://github.com/zyhDeveloper/ZYHModule.gitbash

查詢:pod repo工具

3、建立組件庫(同步驟一操做,在Github或者碼雲上建立)

4、建立組件工程

一、快速建立模版庫

在命令行工具裏cd進入桌面文件夾測試

執行:pod lib create 組件名ui

例:pod lib create XXModulesspa

二、添加組件內容

咱們把基礎組件相關的東西丟到Classes文件夾中,而且把ReplaceMe.m文件刪除命令行

執行:code

git rm XXModule/Classes/ReplaceMe.morm

git add .

git commit -m  'firstCommit'

三、安裝與測試本地庫

在Example項目的Podfile文件中能夠看到 「  pod 'LXFBase', :path => '../'  」,模板庫已經默認幫咱們在Podfile中指定了LXFBase.podspec的位置,咱們無需操做,直接在命令行工具裏cd到Example文件夾,執行pod install

 

四、 修改Spec

5、上傳組件代碼

一、將代碼提交到組件倉庫

git add .
git commit -m 'firstCommit'
git remote add origin https://gitee.com/LinXunFeng/LXFBase.git
git push -f origin master

二、打標籤

標籤與spec中的s.version保持一致

git tag -a 0.1.0 -m "Release version 0.1.0"

git push origin --tags

6、提交podspec到私有索引庫

先作個本地和遠程驗證

一、本地驗證

pod lib lint --allow-warnings

二、遠程驗證

pod spec lint --allow-warnings

三、提交podspec

執行: pod repo push 私有索引庫名稱 spec名稱.podspec
pod repo push ZYHModule XXModule.podspec --allow-warnings

7、使用私有庫

測試下搜索咱們的組件:pod search XXModules

一、添加Podfile文件

cd到開發的項目工程文件

執行:pod init

二、在Podfile的最頂部添加以下描述

// 第二行是爲了保證公有庫的正常使用

source 'https://github.com/zyhDeveloper/ZYHModule.git'

source 'https://github.com/CocoaPods/Specs.git'

添加:pod 'XXModules'

三、安裝組件

執行:pod install

專題二

1、私有庫的更新迭代

更新遠程私有倉庫

一、代碼變更更新

組件庫當有新增文件或者發生修改或者刪除時,更新迭代執行:

刪除:git rm XXXX

git add .

git commit -m '更新描述'

git push origin master

二、版本更新

版本更新 這一步很是重要,爲更新索引庫作準備

git tag -a 0.2.0 -m "Release version 0.2.0"

git push origin --tags

修改描述文件並更新索引庫

一、修改spec文件

打開你的xx.podspec文件,將本來的版本號改成0.2.0,與剛剛的tag保持一致

git add .

git commit -m '更新描述'

git push origin master

二、驗證Spec

pod spec lint --allow-warnings

三、更新索引庫

// pod repo push 索引庫名稱 xxx.podspec

pod repo push ZYHModule XXModule.podspec --allow-warnings

最終需更新使用

pod update 

2、子庫Subspecs的使用

    若是咱們只須要用到SDWebImage中的GIF功能,那麼並不須要將整個SDWebImage都下載下來,在Podfile中將~~pod 'SDWebImage'~~ 改成 pod SDWebImage/GIF便可單獨使用這一功能。

子庫格式

s.subspec '子庫名稱' do |別名|

end

  使用說明:

s.source_files = 'XXModule/Classes/*'
# s.dependency 'SDWebImage', '~> 4.3.3'主庫的依賴庫
s.subspec 'Cache' do |c| 
c.source_files = '
XXModule/Classes/Manager/**/*'
c.dependency 'SDWebImage', '~> 4.3.3' 子庫的依賴庫
end
s.subspec 'Manager' do |m| 
m.source_files = '
XXModule/Classes/Category/**/*'
end

修改後再按以前的步驟更新索引庫和組件庫就能夠了

pod spec lint --allow-warnings(若是文件路徑沒問題,出現The`source_files` pattern did not match any file可嘗試新設置tag)

pod repo push ZYHModule XXModule.podspec --allow-warnings

pod update

如需在某個工程添加子庫,可執行:

pod 'XXModule/Manager'

pod install

專題三

資源文件的使用

資源文件都放在Assets文件夾中

1、修改Spec

將關於資源加載的註釋去掉

s.resource_bundles = {

      # 'LXFMain' => ['LXFMain/Assets/*.png']

      改成:'LXFMain' => ['LXFMain/Assets/*']

}

2、修改加載資源代碼

使用[UIImage imageNamed:@"圖片名稱"]加載不出來圖片

使用如下代碼:

NSString *normalImgName = [NSString stringWithFormat:@ "%@.png", normalImg];
NSBundle *curBundle = [NSBundle bundleForClass:self.class]; 
NSString *curBundleName = curBundle.infoDictionary[@"CFBundleName"];
NSString *curBundleDirectory = [NSString stringWithFormat:@"%@.bundle", curBundleName];
NSString *normalImgPath = [curBundle pathForResource:normalImgName ofType:nil inDirectory:curBundleDirectory];
UIImage *normalImage = [UIImage imageWithContentsOfFile:normalImgPath];
 
 
Xib的加載也是如此
NSBundle *curBundle = [NSBundle bundleForClass:self.class];
CustomView *customView = (CustomView *)[curBundle loadNibNamed:@ "CustomView" owner:nil options:nil].firstObject;
centerView.frame = CGRectMake(30, 140, 200, 100);
[self.view addSubview:customView];

若是是直接在xib中拖入一個imageView控件來設置圖片的加載,咱們則須要在圖片名字前加上當前bundle名稱   XXXX.bundle/圖片名字

專題四

本地私有索引庫路徑:/Users/Mr.z/.cocoapods/repos

 刪除私有索引庫:

pod repo remove [索引庫名稱] //移除本地索引庫
相關文章
相關標籤/搜索