iOS 玩转CocoaPods
####導語:
有時候看到其他人 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參考
-
上傳項目 下載該倉庫,通過git clone 。clone一個倉庫下來 怎么clone如下圖clone with https.
在終端輸入
//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.
- update tag到Github上 上傳tag到Github服務器上,這個比較簡單
- 查看tag 如下圖所示 :點擊branch 然后查看Tags欄目
至此源碼已經上傳到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 = <<-DESCthis 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" }//具體licenses.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 = <<-DESCthis 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 注冊
執行完上面的命令,你的郵箱會收到一封確認信,點擊確認驗證一下就ok啦。
- 判斷podspec正確行?
如果是正確的spec會出現下面的提示:
- 上傳到cocoapod服務器
上傳成功如下圖所示:
- search 你的庫. 網址為:https://cocoapods.org/ 如圖五:search GKFramework ####五,遇到的坑及解決方案
- 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 執行下面命令
如果執行上面的有問題出現 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:
驗證失敗,會出現一系列錯誤,但也不是無根可尋,其中出現錯誤頻率最多的提示是
- 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
總結
以上是生活随笔為你收集整理的iOS 玩转CocoaPods的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SDWebImage 4 0 迁移指南
- 下一篇: 浮点数一些设计原理