iOS 玩轉CocoaPods

wongstar發表於2017-12-14

####導語:

有時候看到其他人 source開源時候用pod xxx 配置在你的Podfile檔案中,執行下pod install 或者 pod update ,程式碼瞬間就到你的pod庫, 頓時覺得高大上。那是怎麼做到的呢?

Agenda:

  • CocoaPods 的由來
  • Github 使用
  • PodSpec介紹
  • PodSpec上傳
  • 遇到的坑及解決方案

###一,CocoaPods 的由來 Android app目前通過gradle來管理和配置你的source,比如需要用到Eventbus只要在build.gradle中配置下

dependencies {
    compile 'org.greenrobot:eventbus:3.0.0'
}
複製程式碼

iOS必須要有類似的神器啊:CocoaPod就是這把神器. 在CocoaPod沒出來之前iOS要用引用第三庫的做法如下: 比如引用AFNetWorking庫,需要去下載原始碼,然後需要配置對應的編譯環境等。當AFNetWorking庫升級,所有過程又來一遍。過程太過複雜啦。 CocoaPods因上面的原因應運而生,它目前是iOS最有名的類庫管理工具了,通過cocoaPods,只需要一行命令就可以完全解決,當然前提是你必須正確設定它。目前絕大部分有名的開源類庫,都支援CocoaPods。所以,作為iOS程式設計師的我們,掌握CocoaPods的使用是必不可少的基本技能了。

如果你mac上沒有安裝pod , sudo gem install cocoapods 具體怎麼安裝可以參考如下連結: http://www.jianshu.com/p/9e4e36ba8574

開發iOS應用用到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 //安裝專案中的依賴專案
pod update -->Update outdated project dependencies and create new Podfile.lock//跟新依賴專案並且更新Podfile.lock
pod init -->Generate a Podfile for the current directory//建立pod file檔案
複製程式碼

其中Podfile.lock的扮演的角色非常重要,具體作用可以參考如下連結 http://m.blog.csdn.net/muzhenhua/article/details/45174071

####二,Github 使用 為嘛要介紹GitHub呢?CocoaPods只是做為專案的具體管理者,podspec檔案就放在cocoapod官網上,供大家搜尋。但是實際原始碼則是儲存在Github上,那怎麼使用Github就非常關鍵啦。

  • 建立專案 登入你的Github,然後去建立一個新的倉庫,如下圖建立GKFramework參考

    image.png

  • 上傳專案 下載該倉庫,通過git clone 。clone一個倉庫下來 怎麼clone如下圖clone with https.

    image.png

在終端輸入

//git clone後面的是你對應的git 地址
git clone https://github.com/wongstar/GKFramework.git

複製程式碼

然後在這個倉庫中修改或者新增你需要對應類或者檔案等。

然後通過下面命令

//add 所有的到倉庫
git add *
//提交commit資訊
git commit
//提交本地到遠端
git push origin master
複製程式碼
  • 打tag tag是後續spec中需要用到,以後升級至需要升級對應tag.
//獲取當前有多少tag
git tag
//建立tag 0.0.1版本
git tag 0.0.1
複製程式碼
  • update tag到Github上 上傳tag到Github伺服器上,這個比較簡單
 git  push origin 0.0.1
複製程式碼
  • 檢視tag 如下圖所示 :點選branch 然後檢視Tags欄目
    image.png

至此原始碼已經上傳到Github伺服器上去了,但是Pod伺服器上目前還沒有對應的描述,下面接著介紹PodSpec,以及如何上傳到cocoapod伺服器上去. ####三,PodSpec介紹

在mac 上建立一個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" //簡介,以後search到簡介
  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"//build的平臺
  s.ios.deployment_target = "7.0"//最低開發

  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 這裡面你定義的描述.必須用這個格式 DESC

  s.source       = { :git => "https://github.com/wongstar/GKFramework.git", :tag => "#{s.version}" }
複製程式碼

必須定義s.source,git連結必須是你上傳過的source, tag定義為你在github上對source打的tag.

s.source_files  = 'Classes/**/*' 定義為:Classes目錄下的所有檔案
複製程式碼
s.dependency:依賴庫,不能依賴未釋出的庫
eg: s.dependency  = 'AFNetworking'
複製程式碼

####四,PodSpec上傳

  • 在 cocoapods 註冊
//email代表你的email,username代表你的使用者名稱
 pod trunk register email "username"
複製程式碼

執行完上面的命令,你的郵箱會收到一封確認信,點選確認驗證一下就ok啦。

  • 判斷podspec正確行?
//GKFramework.podspec為你對應的podspec檔案
pod  spec lint GKFramework.podspec
複製程式碼

如果是正確的spec會出現下面的提示:

image.png

  • 上傳到cocoapod伺服器
//注:GKFramework.podspec為你對應spec的名稱
pod trunk push GKFramework.podspec
複製程式碼

上傳成功如下圖所示:

image.png

  • search 你的庫. 網址為:https://cocoapods.org/ 如圖五:search GKFramework
    圖五.png
    ####五,遇到的坑及解決方案
  • source file沒找到 [iOS] file patterns: The source_files pattern did not match any file. 確保你的source file是否配置正確,如你的spec目錄和source對應的關係
  • cocoapods環境問題 unknown: Encountered an unknown error (Simulator iPhone 4s is not available.) during validation 執行下面命令
sudo gem install cocoapods --pre
複製程式碼

如果執行上面的有問題出現 ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/xcodeproj 執行下面命令

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.
複製程式碼

驗證失敗,會出現一系列錯誤,但也不是無根可尋,其中出現錯誤頻率最多的提示是

  • source files沒找到 ERROR | [iOS] file patterns: The source_files pattern did not match any file.

此錯誤的原因是沒有找到匹配的檔案。

解決方案: 手動建立檔案,具體操作方法如下 終端輸入: open /Users/icepoint/Library/Caches/CocoaPods/Pods/External/GKFramework/035cb9aa62b9d49f904fad1119b83da4-aebfe 進入相應資料夾,建立資料夾與source_files檔案路徑對應 GKFramework/GKFramework/Classes 檔案結構如下: GKFramework └── 035cb9aa62b9d49f904fad1119b83da4-aebfe
├── GKFramework │ └── GKFramework │ └──Classes └── LICENSE #開源協議 預設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

相關文章