利用cocoapods管理開源項目,支持 pod install安裝整個流程記錄(github公有庫),完成預期的任務,大致有下面幾步:
1.代碼提交到github平台
2.創建.podspec
3.編輯.podspec
4.添加tag
5.驗證
6.注冊CocoaPods
7.發布
詳細流程如下:
1.代碼提交到github平台
將自己的代碼上傳到github上面(不是唯一的,上傳到任何平台上都可以,前提是項目是開源的)。至於怎么利用github管理我們的項目,可以參考:http://www.cnblogs.com/richard-youth/p/4684100.html
2.創建.podspec
首先要利用cocoapods將項目管理起來,具體可以參考:http://www.cnblogs.com/richard-youth/p/4508049.html注
yooweiTest這個是你框架的名稱)
$ pod spec create
yooweiTest
3.編輯.podspec
創建好后打開,刪除注釋, 前面有#的為注釋,如果你想知道每個東西的含義可以了解一下
整理之后的文件Pod::Spec.new do |s| s.name = "
yooweiTest"
s.version = "1.0"
s.ios.deployment_target = '7.0'
s.summary = "
yooweiTest"
s.homepage = "
https://github.com/yoowei/yooweiTest"
s.license= { :type => "MIT", :file => "LICENSE" }
s.author = { "yoowei" => "yoowei@126.com" }
s.source = { :git => "
https://github.com/yoowei/yooweiTest.git", :tag => s.version } /注意需要
.git結尾。
s.source_files = "yooweitest", "yooweitest/**/*" //需要和.podspec 的名稱一樣??
接下來講解一下每行代碼的含義
s.name:名稱,pod search 搜索的關鍵詞,注意
這里一定要和.podspec的名稱一樣,否則報錯
s.version:版本號
s.ios.deployment_target:支持的pod最低版本
s.summary: 簡介
s.homepage:項目主頁地址
s.license:許可證
s.author:作者
s.social_media_url:社交網址
s.source:項目的地址
s.source_files:需要包含的源文件
s.resources: 資源文件
s.requires_arc: 是否支持ARC
s.dependency:依賴庫,不能依賴未發布的庫
s.dependency:依賴庫,如有多個可以這樣寫
s.license= { :type => "MIT", :file => "LICENSE" }
這里建議大家這樣寫,如果寫別的會報警告,導致后面一直提交失敗
- source_files:寫法及含義
建議
大家寫第一種或者第二種"yooweitest/* "yooweitest/yoowei/*.{h,m}" "yooweitest/**/*.h"
- “*” 表示匹配所有文件
- “*.{h,m}” 表示匹配所有以.h和.m結尾的文件
- “**” 表示匹配所有子目錄
- s.source 常見寫法
s.source = { :git => "https://github.com/yoowei/yooweiTest.git", :commit => "68defea" } s.source = { :git => "https://github.com/yoowei/yooweiTest.git", :tag => 1.0.0 } s.source = { :git => "https://github.com/yoowei/yooweiTest.git", :tag => s.version }
- commit => "68defea" 表示將這個Pod版本與Git倉庫中某個commit綁定
- tag => 1.0.0 表示將這個Pod版本與Git倉庫中某個版本的comit綁定
- tag => s.version 表示將這個Pod版本與Git倉庫中相同版本的comit綁定
// 引用libxml2.lib和libz.lib.
spec.libraries = 'xml2', 'z'
2、引用公有framework:"(.ios).framework" 用”,”分割. 去掉尾部的”.framework”
spec.frameworks = 'UIKit','SystemConfiguration', 'Accelerate'
3、引用自己生成的framework:’(.ios).vendored_frameworks’ 用”,”分割 路徑寫從.podspec所在目錄為根目錄的相對路徑 ps:這個不要省略.framework spec.ios.vendored_frameworks = 'Pod/Assets/*.framework'
4、引用自己生成的.a文件, 添加到Pod/Assets文件夾里. Demo的Example文件夾里也需要添加一下, 不然找不到
spec.ios.vendored_libraries = 'Pod/Assets/*.a'
注意:在提交到私有倉庫的時候需要加上--use-libraries
4.創建LICENSE(許可證/授權)文件,此文件必須要有
創建一個文件名字命名為LICENSE,內容為:只需要把前面的版權改一下就行了,后面的都一樣。MIT LicenseCopyright (c) 2017Permission 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
$ git tag7.驗證.podspec文件
到此檢查一下你工程下面的文件, 你的項目, .podspec文件, LICENSE文件是否存在。驗證會先測試本地 .podspec 文件是否存在語法錯誤.// --verbose 如果驗證失敗會報錯誤信息
$ pod spec lint yooweiTest.podspec --verbose
yooweiTest.podspec passed validation.
前段時間操作遇到的錯誤和警告:
1、
-> yooweiTest (1.0)
- WARN | github_sources: Github repositories should end in `.git`. //注意需要以.git結尾
- WARN | description: The description is equal to the summary.
- NOTE | xcodebuild: warning: no rule to process file 'yooweiTest/JSPatchtest/yoowei/JSPatch.js' of type sourcecode.javascript for architecture i386
- NOTE | xcodebuild: warning: no rule to process file 'yooweiTest/JSPatchtest/yoowei/JSPatch.js' of type sourcecode.javascript for architecture x86_64
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
$ pod lib lint --allow-warnings
$ echo "2.3" > .swift-version
2、 -> JSPatchtest (1.0.0)
- ERROR | name: The name of the spec should match the name of the file.// 這個是要保證.podspe文件的名稱和里面s.name保持一致
- WARN | github_sources: Github repositories should end in `.git`.
- WARN | description: The description is equal to the summary.
- ERROR | [iOS] unknown: Encountered an unknown error (No podspec found for `JSPatchtest` in `/Users/galahad/Desktop/yooweiTest`) during validation.
[!] JSPatchtest did not pass validation, due to 2 errors and 2 warnings.
3 、
[!] Error installing JSPatchtest
-> yooweitest (1.0.0)
- WARN | description: The description is shorter than the summary.
- ERROR | [iOS] unknown: Encountered an unknown error ([!] /usr/bin/git clone https://github.com/yoowei/yooweiTest.git /var/folders/rg/gmjp61bx0x95t4tjx2hh_3sr0000gn/T/d20170113-4028-cvivmq --template= --single-branch --depth 1 --branch 1.0.0
[!] The spec did not pass validation, due to 1 error and 1 warning.
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
(-switch 后面加上你的Xcode.app包內容里的Developer的文件夾路徑即可).
驗證成功后:
yooweiTest passed validation.
驗證失敗:
[!] The spec did not pass validation, due to 1 error.
如果出現錯誤,就各式各樣,根據描述進行解決就可以了。
8.注冊Trunk
如果要添加到Cocoapods的官方庫了,可以使用trunk工具,具體可以查看官方文檔。如果是私有庫的話,詳見:http://www.cnblogs.com/richard-youth/p/6289015.htmlpod --version
命令查看版本,如果CocoaPods版本低,需要升級。已經注冊過的不需要注冊,怎么看自己有沒有注冊
$ pod trunk me
- Name: yoowei
// 加上--verbose可以輸出詳細debug信息,方便出錯時查看。
$ pod trunk register yoowei@126.com "yoowei" --verbose
補充:
我們顯示隱藏文件后,在用戶目錄下,找到 /Users/galahad/.rvm
直接將隱藏文件.rvm刪除9.發布
發布時會驗證 Pod 的有效性,如果你在上面步驟中手動驗證 Pod 時使用了 --use-libraries 或 --allow-warnings 等修飾符,那么發布的時候也應該使用相同的字段修飾,否則出現相同的報錯。pod trunk push yooweiTest.podspec
--allow-warnings
成功了以后會出現以下內容

這個時候,再用
$ pod search yoowei
[!] Unable to find a pod with name, author, summary, or description matching `yoowei`
pod setup
成功后會生成~/Library/Caches/CocoaPods/search_index.json
文件。
終端輸入rm ~/Library/Caches/CocoaPods/search_index.json
刪除成功后再執行pod search 一切OK🌶
$ rm ~/Library/Caches/CocoaPods/search_index.json
$ pod search yoowei
Creating search index for spec repo 'master'.. Done!
$ pod search yoowei
-> yooweiTest (1.0)
yooweiTest just test.
pod 'yooweiTest', '~> 1.0'
- Homepage: https://github.com/yoowei/yooweiTest
- Source: https://github.com/yoowei/yooweiTest.git
- Versions: 1.0 [master repo]

可以利用 pod trunk info yooweiTest 查詢相關信息
