導(dǎo)語:
有時(shí)候看到其他人 source開源時(shí)候用pod xxx 配置在你的Podfile文件中,執(zhí)行下pod install 或者 pod update ,代碼瞬間就到你的pod庫, 頓時(shí)覺得高大上。那是怎么做到的呢?
Agenda:
- CocoaPods 的由來
- Github 使用
- PodSpec介紹
- PodSpec上傳
- 遇到的坑及解決方案
一,CocoaPods 的由來
Android app目前通過gradle來管理和配置你的source,比如需要用到Eventbus只要在build.gradle中配置下
dependencies {
compile 'org.greenrobot:eventbus:3.0.0'
}
iOS必須要有類似的神器?。篊ocoaPod就是這把神器.
在CocoaPod沒出來之前iOS要用引用第三庫的做法如下:
比如引用AFNetWorking庫,需要去下載源碼,然后需要配置對(duì)應(yīng)的編譯環(huán)境等。當(dāng)AFNetWorking庫升級(jí),所有過程又來一遍。過程太過復(fù)雜啦。
CocoaPods因上面的原因應(yīng)運(yùn)而生,它目前是iOS最有名的類庫管理工具了,通過cocoaPods,只需要一行命令就可以完全解決,當(dāng)然前提是你必須正確設(shè)置它。目前絕大部分有名的開源類庫,都支持CocoaPods。所以,作為iOS程序員的我們,掌握CocoaPods的使用是必不可少的基本技能了。
如果你mac上沒有安裝pod ,
sudo gem install cocoapods
具體怎么安裝可以參考如下鏈接:
http://www.itdecent.cn/p/9e4e36ba8574
開發(fā)iOS應(yīng)用用到pod 的主要命令如下:
pod help-->查看pod命令的幫助
pod search --> Search for pods(搜索pod)
pod trunk --> Interact with the CocoaPods API (e.g. publishing new specs) 上傳source到trunk
pod spec --> Manage pod specs//管理pod spec
pod install -->Install project dependencies according to versions from a Podfile.lock //安裝項(xiàng)目中的依賴項(xiàng)目
pod update -->Update outdated project dependencies and create new Podfile.lock//跟新依賴項(xiàng)目并且更新Podfile.lock
pod init -->Generate a Podfile for the current directory//創(chuàng)建pod file文件
其中Podfile.lock的扮演的角色非常重要,具體作用可以參考如下鏈接
http://m.blog.csdn.net/muzhenhua/article/details/45174071
二,Github 使用
為嘛要介紹GitHub呢?CocoaPods只是做為項(xiàng)目的具體管理者,podspec文件就放在cocoapod官網(wǎng)上,供大家搜索。但是實(shí)際源碼則是存儲(chǔ)在Github上,那怎么使用Github就非常關(guān)鍵啦。
-
創(chuàng)建項(xiàng)目
登錄你的Github,然后去創(chuàng)建一個(gè)新的倉庫,如下圖創(chuàng)建GKFramework參考
image.png -
上傳項(xiàng)目
下載該倉庫,通過git clone 。clone一個(gè)倉庫下來
怎么clone如下圖clone with https.
image.png
在終端輸入
//git clone后面的是你對(duì)應(yīng)的git 地址
git clone https://github.com/wongstar/GKFramework.git
然后在這個(gè)倉庫中修改或者添加你需要對(duì)應(yīng)類或者文件等。
然后通過下面命令
//add 所有的到倉庫
git add *
//提交commit信息
git commit
//提交本地到遠(yuǎn)端
git push origin master
- 打tag
tag是后續(xù)spec中需要用到,以后升級(jí)至需要升級(jí)對(duì)應(yīng)tag.
//獲取當(dāng)前有多少tag
git tag
//創(chuàng)建tag 0.0.1版本
git tag 0.0.1
- update tag到Github上
上傳tag到Github服務(wù)器上,這個(gè)比較簡(jiǎn)單
git push origin 0.0.1
-
查看tag
如下圖所示 :點(diǎn)擊branch 然后查看Tags欄目
image.png
至此源碼已經(jīng)上傳到Github服務(wù)器上去了,但是Pod服務(wù)器上目前還沒有對(duì)應(yīng)的描述,下面接著介紹PodSpec,以及如何上傳到cocoapod服務(wù)器上去.
三,PodSpec介紹
在mac 上創(chuàng)建一個(gè)podspec,在Terminal終端上輸入下面命令:
//注GKFramework.podspec是你的框架名稱
pod spec create GKFramework.podspec
然后編輯podspec文件。如下GKFramework.podspec
Pod::Spec.new do |s|
s.name = "GKFramework" //定義名稱
s.version = "0.0.5" //tag版本
s.summary = "A Sample.so you can use it" //簡(jiǎn)介,以后search到簡(jiǎn)介
s.description = <<-DESC
this is gk framework, use it for test your framework. we can use it as framework.
DESC
//s.description 定義具體的描述
s.homepage = "https://github.com/wongstar/GKFramework"
s.license = { :type => "MIT", :file => "LICENSE" }//具體license
s.author = { "wongstar" => "wongstar.iac@gmail.com" }
s.platform = :ios, "8.0"http://build的平臺(tái)
s.ios.deployment_target = "7.0"http://最低開發(fā)
s.source = { :git => "https://github.com/wongstar/GKFramework.git", :tag => "#{s.version}" }
s.source_files = 'Classes/**/*'
#s.public_header_files='GKFramework/Classes/**/*.h'
end
s.description = <<-DESC
this is gk framework, use it for test your framework. we can use it as framework.
DESC
s.description定義了描述該pod是用來做什么的。注意這里的寫法
s.description格式要求必須是下面的這樣描述
<<-DESC
這里面你定義的描述.必須用這個(gè)格式
DESC
s.source = { :git => "https://github.com/wongstar/GKFramework.git", :tag => "#{s.version}" }
必須定義s.source,git鏈接必須是你上傳過的source, tag定義為你在github上對(duì)source打的tag.
s.source_files = 'Classes/**/*' 定義為:Classes目錄下的所有文件
s.dependency:依賴庫,不能依賴未發(fā)布的庫
eg: s.dependency = 'AFNetworking'
四,PodSpec上傳
- 在 cocoapods 注冊(cè)
//email代表你的email,username代表你的用戶名
pod trunk register email "username"
執(zhí)行完上面的命令,你的郵箱會(huì)收到一封確認(rèn)信,點(diǎn)擊確認(rèn)驗(yàn)證一下就ok啦。
- 判斷podspec正確行?
//GKFramework.podspec為你對(duì)應(yīng)的podspec文件
pod spec lint GKFramework.podspec
如果是正確的spec會(huì)出現(xiàn)下面的提示:

- 上傳到cocoapod服務(wù)器
//注:GKFramework.podspec為你對(duì)應(yīng)spec的名稱
pod trunk push GKFramework.podspec
上傳成功如下圖所示:

- search 你的庫.
網(wǎng)址為:https://cocoapods.org/
如圖五:search GKFramework
圖五.png
五,遇到的坑及解決方案
- source file沒找到
[iOS] file patterns: Thesource_filespattern did not match any file.
確保你的source file是否配置正確,如你的spec目錄和source對(duì)應(yīng)的關(guān)系 - cocoapods環(huán)境問題
unknown: Encountered an unknown error (Simulator iPhone 4s is not available.) during validation
執(zhí)行下面命令
sudo gem install cocoapods --pre
如果執(zhí)行上面的有問題出現(xiàn)
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/xcodeproj
執(zhí)行下面命令
sudo gem install -n /usr/local/bin cocoapods
- Swift 版本問題
[!] 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.
驗(yàn)證失敗,會(huì)出現(xiàn)一系列錯(cuò)誤,但也不是無根可尋,其中出現(xiàn)錯(cuò)誤頻率最多的提示是
- source files沒找到
ERROR | [iOS] file patterns: Thesource_filespattern did not match any file.
此錯(cuò)誤的原因是沒有找到匹配的文件。
解決方案:
手動(dòng)創(chuàng)建文件,具體操作方法如下
終端輸入:
open /Users/icepoint/Library/Caches/CocoaPods/Pods/External/GKFramework/035cb9aa62b9d49f904fad1119b83da4-aebfe
進(jìn)入相應(yīng)文件夾,創(chuàng)建文件夾與source_files文件路徑對(duì)應(yīng)
GKFramework/GKFramework/Classes
文件結(jié)構(gòu)如下:
GKFramework
└── 035cb9aa62b9d49f904fad1119b83da4-aebfe
├── GKFramework
│ └── GKFramework
│ └──Classes
└── LICENSE #開源協(xié)議 默認(rèn)MIT
Classes文件夾存放自己的庫文件
- pod search GKFramework 搜索不到
Unable to find a pod with name, author, summary, or description matching GKFramework.
解決辦法:
1.pod install --repo-update
2.或者全部刪除:使用命令:rm ~/Library/Caches/CocoaPods/search_index.json
重新search GKFramework



