設計網(wǎng)頁推薦萬秀服務不錯的seo推廣
一、創(chuàng)建私有庫索引
步驟1是在沒有索引庫的情況下或者是新增索引的時候才需要用到(創(chuàng)建基礎組件庫)
首先在碼云上建立一個私有庫索引,起名為SYComponentSpec
二、本地添加私有庫索引
添加私有庫索引
pod repo add SYComponentSpec https://gitee.com/sun-shiyu/sycomponent-spec
三、創(chuàng)建基礎組件庫
當你需要新建組件的時候,就在這里開始,如果是想修改原有的庫,就可以直接在之前的組件倉庫里面修改即可
1.在碼云上創(chuàng)建組件庫
命名為SYBasicComponents
,如圖:
2.創(chuàng)建SYBasicComponents
本地庫
默認創(chuàng)建路徑:/Users/sunshiyu/SYBasicComponents
。
pod lib create SYBasicComponents
最后,項目本地組件庫創(chuàng)建完成后會自動打開項目。
3.在私有庫導入自己的代碼
將Classes
文件夾下面的ReplaceMe.m
文件刪除掉,替換成你要上傳私有庫的代碼,這里導入簡單的兩個測試文件:
// SYLog.h
+ (void)logger;
// SYLog.m
+ (void)logger {NSLog(@"-------- Log --------");
}
4.更新這個工程的pod庫
cd
到Example
文件下- 執(zhí)行
pod install
5.修改.podspec
文件
有兩個屬性需要注意改下:
s.homepage
:私有代碼倉庫的地址 https://gitee.com/sun-shiyu/sybasic-components
s.source
:私有代碼倉庫的源地址 https://gitee.com/sun-shiyu/sybasic-components.git
其他屬性根據(jù)需要自行配置。
6.將私有庫push到遠程倉庫
注意遠端需要有個master
分支,這里先創(chuàng)建一個master
分支:
cd /Users/sunshiyu/SYBasicComponents
git branch master
git checkout master
commit代碼到本地然后push到遠端:
git branch (檢查當前所在分支)
git status (查看當前git存了什么文件)
git add . (將所有文件緩存到待提交文件區(qū)域)
git commit -m "上傳組件"
git remote add origin https://gitee.com/sun-shiyu/sybasic-components.git (私有庫git倉庫地址)
git push -f origin master (將代碼推送到遠程私有庫git倉庫的master分支上面已經創(chuàng)建了)
git tag 0.1.0 (這里的版本號必須和podspec里面寫的版本號一致)
git push -- tags (提交tag)
7.本地和遠程校驗
1.本地校驗,在當前私有庫目錄下,輸入命令:
pod lib lint --private --allow-warnings
校驗成功:SYBasicComponents passed validation.
2.遠程驗證,在當前私有庫目錄下,輸入命令:
pod spec lint --private --allow-warnings
校驗成功:SYBasicComponents.podspec passed validation.
8.提交索引文件到遠程索引庫
1.所有驗證通過之后,要將spec文件推送到最開始創(chuàng)建的私有庫索引庫當中
cd 到私有庫項目目錄,
pod repo push <本地索引庫名稱> <索引文件名> --allow-warnings
<本地索引庫名稱>在 /Users/sunshiyu/.cocoapods/repos 下的私有庫索引項目名稱
<索引文件名>就是以 podspec 結尾的,注意不要弄錯
例如輸入命令:
pod repo push SYComponentSpec SYBasicComponents.podspec --allow-warnings
2.推送去之后,在本地索引庫中查看如下圖??????????????:
在 getee
遠端查看如下圖:
四、使用基礎組件庫
隨便創(chuàng)建一個項目名為 SYSpecDemo
,初始化 pod
:
cd /Users/sunshiyu/Desktop/SYSpecDemo
pod init
pod install
打開編輯 podfile
文件
open podfile
如下:
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'# source 添加對應的索引庫,否則會pod install失敗
source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'target 'SYSpecDemo' do# Comment the next line if you don't want to use dynamic frameworksuse_frameworks!# Pods for SYSpecDemo# pod 對應組件,可以對應版本pod 'SYBasicComponents'end
再次:
pod install
就可以看到這個庫:
導入頭文件使用該庫:
#import <SYBasicComponents/SYLog.h>// 打印 log
[SYLog logger];
五、組件庫依賴第三方庫
如果我們的組件庫需要依賴第三方庫,例如 AFNetWorking
、YYModel
等,操作如下:
1.修改組件庫的 SYBasicComponents.podspec
文件,添加依賴:
然后組件庫執(zhí)行 Example
項目 執(zhí)行 pod install
。
2.同上面第6步,將組件庫修改的代碼提到遠端。記住一定要記得打 tag
,且要與索引庫的 version
保持一致 。
git tag 0.2.0 (這里的版本號必須和podspec里面寫的版本號一致)
git push -- tags (提交tag)
3.提交索引庫遠端,同上面的第8步:
pod repo push SYComponentSpec SYBasicComponents.podspec --allow-warnings
4.修改主項目的 podfile
文件,加入 github CocoaPods
索引庫,用來加載github
三方庫的:
source 'https://github.com/CocoaPods/Specs.git'
然后執(zhí)行 pod install
。
以上是使用 pod 'SYBasicComponents'
的方式使用組件庫,也就是遠程的方式,這種方式需要沒更新組件庫后都要打 tag
以及修改 spec
的version
,然后修改主項目的 podfile
文件(根據(jù)情況也可能不需要要修改),最后主項目重新 pod install
。這樣就能得到遠程組件庫最新的代碼,缺點是主項目每次想更新組件庫的最近代碼都需要組件庫打 tag
更新組件庫的版本,如何實現(xiàn)只要組件庫有代碼更新,主項目直接拉最新的代碼呢?答案是引用本地索引庫的方式,我們只需要修改主項目的 podfile
文件如下:
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'# source 添加對應的索引庫,否則會 pod install 失敗
# source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'
source 'https://github.com/CocoaPods/Specs.git'target 'SYSpecDemo' do# Comment the next line if you don't want to use dynamic frameworksuse_frameworks!# Pods for SYSpecDemo# pod 對應組件,可以對應版本# 方式一、引用遠端組件庫# pod 'SYBasicComponents'# 方式二:引用本地組件庫,不需要 source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'pod 'SYBasicComponents', :path => '~/SYBasicComponents/'end
即使用 pod 'SYBasicComponents', :path => '~/SYBasicComponents/'
的方式來直接引用本地組件庫的代碼,這樣拉代碼每次都是最新的。我們 pod install
試試看,Pods
目錄結構變如下圖所示:
本地組件庫 SYBasicComponents
里面就是最新的代碼了。因為我們的組件庫依賴了 AFNetworking
YYModel
,所以Pods
中出現(xiàn)了這兩個庫。
結束!!!
參考:
iOS私有庫搭建
iOS組件化搭建私有庫