使用Cocoapods來管理一個自己的私有庫,在做項目的時候,可以方便我們自己管理和使用。想象一下,如果你的代碼庫在多個項目中使用,其中一個文件更新,你需要在多少個項目中修改文件?而使用Cocoapods只需pod update一下。。。
下面介紹一下創建私有庫的詳細過程。
1、注冊Trunk
trunk需要CocoaPods 0.33版本以上,用
pod --version
命令查看版本
如果版本低,需要升級:
sudo gen install cocoapods
pod setup
查看自己是否注冊過Trunk
pod trunk me
沒有注冊過會提示:You need to register a session first.
注冊
// 加上--verbose可以輸出詳細debug信息,方便出錯時查看。
pod trunk register xxxxx@163.com "name" --verbose
"name" 里面代表你的用戶名,最好起一個好的名字
xxxxx@163.com 代表你的郵箱
注冊完成之后會給你的郵箱發個郵件,進入郵箱郵件里面有個鏈接,需要點擊確認一下
確認結果:
注冊成功后可以再查看一下個人信息
pod trunk me
2、創建一個項目,添加到sourcetree創建本地倉庫
3、創建.podspec
cd 到你的項目下
// 注 SYCommonTool 這個是你框架的名稱
1、pod spec create SYCommonTool
編輯.podspec文件
Pod::Spec.new do |spec|
spec.name = "SYCommonTool"
spec.version = "0.0.1"
spec.summary = "A just test demo"
spec.description = <<-DESC
創建項目中常用組件的集合,不斷更新
DESC
注意: description不能太短
spec.homepage = "[https://github.com/lisiyuan1993/SYCommonTool](https://github.com/lisiyuan1993/SYCommonTool)"
spec.license = "MIT"
# spec.license = { :type => "MIT", :file => "FILE_LICENSE" }
spec.author = { "lisiyuan" => "184275267@qq.com" }
spec.platform = :ios, "8.0"
spec.source = { :git => "[https://github.com/lisiyuan1993/SYCommonTool](https://github.com/lisiyuan1993/SYCommonTool).git", :tag => "#{spec.version}" }
spec.source_files = "SYCommonTool/**/*.{h,m}"
source_files: 寫法
'SYCommonTool/*' '*'表示匹配所有文件
'SYCommonTool/SYCommonTool/*.{h,m}' '*.{h,m}' 表示匹配所有以.h和.m結尾的文件
'SYCommonTool/**/*.h' '**' 表示匹配所有子目錄
注意--常常遇到這個報錯:file patterns: The `source_files` pattern did not match any file.
這個錯誤也是使用指令pod lib lint SYCommonTool.podspec檢查文件是否合法時發生的;
這個是在指定共享的類庫時, 文件路徑不對, 也就是設置s.source_files字段時, 發生了錯誤, 這里的路徑是相對于SYCommonTool.podspec文件的, 如果是與SYCommonTool.podspec同級的文件夾, 直接寫文件夾名稱即可, 如:
s.source_files = "SYCommonTool"
如果有多級目錄, 一定要逐級-逐級-逐級添加. 這里也可以這么寫:
s.source_files = "SYCommonTool/**/*.{h,m}"
spec.exclude_files = "Classes/Exclude"
end
4、如果前面沒有選擇創建這個LICENSE文件, 創建LICENSE(許可證/授權)文件,此文件必須要有
創建一個文件名字命名為LICENSE,內容為:只需要把前面的版權改一下就行了,后面的都一樣
Copyright (c) 2019 nuckyLee
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5、上傳到Git
將包含配置好的 .podspec, LICENSE 的項目提交 Git
6、打tag
查看所有的tag
$ git tag
git tag 0.0.1 添加一個tag
將0.0.1標簽提交到git服務器
$ git push origin 0.0.1
添加新的release版本
$ git add . && git commit -m 'Release 0.0.1'
將本地所有標簽一次性提交到git服務器
$ git push origin –tags
$ git push --tags
如果我們的tag打錯了,沒有用的話,我們應該怎么刪除呢?
1、查看tags
git tag 就會羅列出我們所有的tags
2、刪除本地tags
git tag -d tagName
3、刪除遠程分支
git push origin :refs/tags/分支名稱(如:0.0.1) 就刪除了遠程分支
我之前沒有執行如下兩條set命令也push成功,但這一次一直不成功(提示"The source_filespattern did not match any file."錯誤),于是在網上有人說到要執行如下兩條命令,果然成功了,因為當時我的文件路徑(本地和網絡git上),確定是沒有錯的。
set the new version to 1.0.0
set the new tag to 1.0.0 (1.0.0要與podspec的version對應)
這兩條命令是為pod添加版本號并打上tag。然后執行pod驗證命令:
$ pod lib lint
這里要注意一點,有時你寫的podspec文件在后面push的時候會提示[iOS] file patterns: Thesource_filespattern did not match any fi錯誤,網上很多人都說是路徑不對!其實在確認你的路徑沒問題的情況下還報這個錯,那就git 對應的tag下根本沒有對應的podspec文件;
你進入到git 主頁,按下圖的方式點擊對應的tag
進入對應的tag后,你后發現根本沒有任何文件,所以肯定不會成功;
7、上傳代碼到Cocoapods
以上全部完成后, 最后一步就是上傳代碼到Cocoapods了;
首先, 使用下面的指令來檢查文件是否可用:
pod spec lint
出現錯誤: - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
- ERROR | xcodebuild: ....
- error: include of non-modular header inside framework module '' [-Werror,-Wnon-modular-include-in-framework-module]
...
原因就是我的庫的某個頭文件中直接import了第三方庫(我對它有依賴)的頭文件,我修改了spec.frameworks = "Foundation", "UIKit"解決。
如果沒有錯誤, 會輸出:
最后就是上傳代碼了, 使用下面的指令進行上傳:
pod trunk push SYCommonTool.podspec
上傳的時間可能會有點長, 耐心等待, 成功后會輸出:
8.測試自己的cocoapods
這個時候使用pod search搜索的話會提示搜索不到,可以執行以下命令更新本地search_index.json文件
rm ~/Library/Caches/CocoaPods/search_index.json
然后
pod search SYCommonTool
該命令會重新創建search_index.json文件,5-10分鐘,耐性等待
!!!
不管你的spec版本是否已經發布過一版還是第一次發布,在你改了項目里面的代碼后你都必須像更新git代碼一樣先更新tag,只不過命令不同,具體如下:
1、進入到工程有podspec文件的目錄
2、git tag 0.0.1 添加一個tag
3、git add . && git commit -m 'Release 0.0.1' 添加新的release版本
4、git push --tags 在github上添加tag并更新內容
5、git push origin master 更新git、podspec中的version和tag
6、提交到cocopods:
pod trunk push pod search SYCommonTool.podspec
參考:
[Cocoapods]使用Cocoapods + Github托管代碼[http://www.lxweimin.com/p/a72a529dc659]
(http://www.lxweimin.com/p/a72a529dc659)
手把手教你發布自己的cocoapods開源庫http://www.lxweimin.com/p/3a365f273439
[Cocoapods]項目添加Cocoapods支持遇到的坑http://www.lxweimin.com/p/283584683b0b