使用cocoapods
私有庫能夠更好的幫助我們實現組件化開發,大體分為五部分+坑??
1.私有倉庫pod repo
的創建
- 私有倉庫本地安裝
$ pod repo add YourRepo xxx.xxx.git
YourRepo
是你自己私有庫的名稱
xxx.xxx.git
是你私有庫的地址
私有庫是用來存放podspec
索引文件的倉庫,不存放代碼,一個私有庫可以對應N
個podspec
- 查看本地私有倉庫
$ cd ~/.cocoapods/repos
$ ls
或者
$ pod repo
可查看此文件夾下,除了官方的master
倉庫,還會有自己剛剛安裝的YourRepo
私有倉庫。
- 私有倉庫的移除
pod repo remove [name]
2.代碼倉庫的創建
- cd 本地創建的文件夾
- 使用cocoapods命令:pod lib create xxxxLib
xxxxLib
是你自己組件代碼的名稱
這個命令會自動生成一套組件代碼工程測試代碼,并且有Git
管理
還會生成podspec
索引文件.
- 根據出現的下拉菜單,按需填寫
上面依次對應平臺類型 、 語言 、 Demo 、 測試框架 、 界面測試 、 類前綴 等,填完之后Enter,會生成如下圖的工程目錄代碼。
把 ReplaceMe.m 文件替換你自己的組件代碼
cd 到 Example 然后執行 pod install/pod update
需要需要重新
pod install
,因為不重新pod install
,Example
工程根本不知道Pod
更新了,pod install
的作用:重新讓pod
庫與所依賴的工程文件產生關聯。
- cd 到 NJFLibTest 把代碼上傳到遠程倉庫
注意文件層級關系
NJFLibTest
不是與Example
同級的文件,是上一層文件 ,我們可以用command+shift+.
查看,一般是有.git
隱藏文件的上一級文件
遠程倉庫不需要創建gitignore
文件,因為pod lib
創建了
提交自己倉庫代碼到遠程倉庫
git status : 查看狀態,如果有不想要的文件,可以用gitignore忽略掉
提交到本地緩存區 $git add .
提交到本地倉庫 $git commit -m "初始化提交"
查看遠程倉庫地址 $git remote -v(查看有沒有遠程地址)
綁定遠程地址 $git remote add origin xxxx.xxx.git
推送自己代碼到遠程倉庫 $git push -u origin master
刪除遠程倉庫地址 $git remote rm origin
如果推送成功,可以在遠程倉庫看到自己的提交
- 添加tag
$ git tag -a 0.0.1 -m '0.1.0'
$ git push --tags
查看本地tag
$ git tag
需要注意的是,這個
tag
需要與podspec
里的version
號一致,否則在提交podspec
到pod
遠程倉庫的時候會出錯。
- 修改podspec索引文件
Pod::Spec.new do |s|
s.name = 'NJFLibTest'
s.version = '0.0.1' ///'注意,要與你打的tag一致'
s.summary = 'A short description of NJFLibTest.'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/XXXXX/NJFLibTest' ///'后綴不要帶.git'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Miko-J' => 'niujf@kingnet.com' }
s.source = { :git => 'https://github.com/XXXX/NJFLibTest.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'NJFLibTest/Classes/**/*'
# s.resource_bundles = {
# 'NJFLibTest' => ['NJFLibTest/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 3.2.1'
s.version
對應的版本號就是上面打的tag
s.homepage
和s.source
里面的路徑要替換成你的 遠程倉庫地址
**表示所有文件
*表示通配符,可有可無.
- 執行$ pod lib lint 本地檢查pod spec合法性
如果要出現
error
要修改,如果只有警告可以使用
$ pod lib lint --allow-warnings
如果出現NJFLibTest passed validation
則說明驗證通過
- 執行$ pod spec lint 遠程檢查pod spec合法性
如果要出現
error
要修改,如果只有警告可以使用
$ pod spec lint --allow-warnings
如果出現NJFLibTest.podsepc passed validation
則說明驗證通過
3.把自己私有庫的索引podsepc添加到自己私有庫中
$ pod repo push YourRepo ~/Desktop/xxxx/NJFLibTest.podspec
如果執行成功,刷新一下遠程索引庫,可以看到你更新的NJFLibTest/0.01
了
4.新建項目repoTest,引用自己的私有索引倉庫
在Podfile
文件中編輯如下
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
use_frameworks!
source 'https://github.com/xxxxxx/xx.git' //自己私有庫source源
source 'https://github.com/CocoaPods/Specs.git' //官方的source源
target 'repoTest' do
pod 'NJFLibTest' //自己創建的組件
end
然后pod install就可以了??
5.進階
- 更新代碼倉庫或者添加依賴庫
更新代碼倉庫:很簡單,從 ReplaceMe.m
文件替換你自己的組件代碼,這里開始,跟上面的步驟一樣
1.更新代碼
2.進Example
,執行pod update
3.提交代碼到遠程倉庫
4.打tag
,更新.podspec
文件,檢查pod sepc
的合法性
5.添加索引到自己的私有庫中
添加依賴庫: 打開.podspec
文件 執行s.dependency 'AFNetworking', '~> 3.2.1'
- 子庫Subspecs
先看一個簡單的例子:
$ pod search 'SDWebImage'
可以看到,如果我們只需要用到SDWebImage
中的Core
功能,那么并不需要將整個SDWebImage
都下載下來,在Podfile
中將pod 'SDWebImage'
改為 pod SDWebImage/Core
即可單獨使用這一功能
子庫格式:
s.subspec '子庫名稱' do |別名|
end
因為這里已經分離出子庫了,所以s.source_files
和s.dependency
就不能這么使用了,需要我們在子庫里分別指定,所以我們直接把原來的s.source_files
和s.dependency
都注釋掉。寫法參考如下:
# s.source_files = 'NJFLibTest/Classes/**/*'
# s.dependency 'AFNetworking', '~> 3.2.1'
s.subspec 'ThirdLibs' do |t|
t.source_files = 'NJFLibTest/Classes/ThirdLibs/**/*'
t.dependency 'AFNetworking', '~> 3.2.1'
end
s.subspec 'Category' do |c|
c.source_files = 'NJFLibTest/Classes/Category/**/*'
end
s.subspec 'Configuration' do |c|
c.source_files = 'NJFLibTest/Classes/Configuration/**/*'
end
如果更新索引庫后,可以在遠程索引庫中看到最新的版本
每次改動記得
tag
值要變一下,如果執行$ pod spec lint
遇到錯誤file patterns: The source_files pattern did not match any file.
更新tag
在嘗試一下
搜索私有庫
$ pod search NJFLibTest
可以看到最終結果:
-> NJFLibTest (0.0.3)
A short description of NJFLibTest.
pod 'NJFLibTest', '~> 0.0.3'
- Homepage: https://github.com/XXXXXX
- Source: https://github.com/XXXXX.git
- Versions: 0.0.3, 0.0.1 [NJF_PrivateRepo repo]
- Subspecs:
- NJFLibTest/AFNTool (0.0.3)
- NJFLibTest/Category (0.0.3)
- NJFLibTest/Configuration (0.0.3)
在項目中如果引用所有的庫目錄結構應該是這個樣子,引用個別子庫我就不貼了,大家試一下就知道了
可能遇到的坑
- 沖突
To https://github.com/Miko-J/NJF_RepoCode.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/Miko-J/NJF_RepoCode.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
遇到這種情況,我們先拉一下遠程倉庫的代碼
$ git pull origin master
如果發現
fatal: refusing to merge unrelated histories //'分支拒絕了無歷史關聯的合并'
我們可以使用下面的命令
$ git pull origin master --allow-unrelated-histories
這個時候我們要查看一下狀態
$ git status
如果有沖突,就解決沖突
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both added: .gitignore
both added: LICENSE
這里lz
簡單粗暴直接本地文件覆蓋遠程
$ git push -u origin master -f
如果能提交,可以刷新一下遠程倉庫的代碼看一下
- 庫的索引
podsepc
添加到自己私有庫中出現XXXXXX.podspec specification does not validate.
$ pod repo push YourRepo ~/Desktop/xxxx/NJFLibTest.podspec --verbose --use-libraries --allow-warnings