CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over ten thousand libraries and can help you scale your projects elegantly. - 摘錄自CocoaPods.orghtml
在CocoaPods出現以前,iOS項目中引用第三方庫的方式是很是原始的,要麼是把源代碼拷貝到主工程中,要麼是經過靜態庫引入.a文件,而後還要修改一系列的build settings。後續的第三方庫的升級也是個枯燥乏味的事情,總之若是你的iOS項目目前仍是這樣管理第三方庫,那麼大家還處在石器時代。java
CocoaPods經過集中式的管理,能夠很是有效的管理第三方庫,甚至能夠用於大型項目的模塊化管理,很是優雅高效的解決iOS項目中的依賴管理。ios
CocoaPods是一個Ruby Gem,由於直接訪問RubyGem速度很是慢,建議先替換成淘寶鏡像git
$ gem sources --remove https://rubygems.org/ $ gem sources -a https://ruby.taobao.org/
安裝CocoaPodsgithub
$ sudo gem install cocoapods
在項目根目錄下建立Podfile,下面是一個Podfile的例子 (詳情能夠參考http://guides.cocoapods.org/syntax/podfile.html#podfile):ruby
platform :ios, '9.0' target "MyApp" do pod 'ObjectiveSugar', '~> 0.5' target "MyAppTests" do pod 'OCMock', '~> 2.0.1' end end
platform: 能夠指定平臺的信息和deployment target的版本bash
target: 能夠根據不一樣的target來引入不一樣的pod微信
pod: 引入依賴庫架構
pod 'SSZipArchive' -- 引入最新版本ide
pod 'Objection', '0.9' -- 引入特定的版本
pod 'Objection', '>0.9'> -- 任何大於0.9的版本
pod 'Objection', '>=0.9'> -- 任何大於等於0.9的版本
pod 'Objection', '<0.9'> -- 任何小於0.9的版本
pod 'Objection', '<=0.9'> -- 任何小於等於0.9的版本
pod 'Objection', '~>0.9'> -- 任何介於0.9到1.0的最新版本,不包含1.0
pod 'AFNetworking', :path => '~/Documents/AFNetworking' -- 使用本地路徑引入
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0' -- 使用git庫引入
pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec' -- 使用外部的podspec來引入
安裝pods
$ pod install
更新pods
$ pod update
install和update的區別:假如使用 pod 'SVProgressHUD',沒有指定版本。使用pod install,若是Pods中存在SVProgressHUD,則直接使用。使用pod update,則會保證更新SVProgressHUD到最新版本。
install或update速度一般很慢,由於每次執行的時候都須要同步一下CocoaPods Specs,這個有幾百兆的大小,同步一次很是耗時。因此若是你使用的第三方庫並非常常更新,則不用常常更新那個Specs庫。可使用如下命令:
$ pod install --verbose --no-repo-update $ pod update --verbose --no-repo-update
執行完install或者update命令後,就可使用.xcworkspace打開項目。
隨着iOS APP愈來愈複雜,功能愈來愈多,對於iOS項目的工程化要求也愈來愈高了,對於複雜的APP通常都須要對項目進行模塊化管理。
模塊化有幾個方式:
1. 目錄結構管理:這是最原始的方式,僅僅經過目錄結構實現代碼層次的清晰化。但本質上並無解決代碼之間的依賴混亂的狀況,模塊化劃分也很是不清晰。
2. 子工程:經過子工程能夠實現代碼依賴管理和模塊化,可是須要引入複雜的設置,不利於管理。
3. 靜態庫:將依賴代碼打包成爲靜態庫.a,不過因爲不能看到源碼,調試不方便。
自從有了CocoaPods,可使用它來管理私有庫,從而實現了代碼模塊化管理。例以下圖所示:
例如在github上面建立一個空的git庫:https://github.com/xxx/MySpecs
將這個git庫加入到CocoaPods庫的列表中:
$ pod repo add MySpecs git@github.com:xxx/MySpecs.git
此時能夠檢查下本地的pod repo
$ pod repo list
MySpecs
- Type: git (master)
- URL: git@github.com:xxx/MySpecs.git
- Path: /Users/xxx/.cocoapods/repos/mySpecs
master
- Type: git (master)
- URL: git@github.com:CocoaPods/Specs.git
- Path: /Users/xxx/.cocoapods/repos/master
肯定私有庫的Specs已經加到本地pod repo中。
在私有庫項目中的根目錄,建立對應的podspec文件,裏面會描述這個庫的基本信息。
PodSpec規範能夠查看:https://guides.cocoapods.org/syntax/podspec.html
# # Be sure to run `pod spec lint PodName.podspec' to ensure this is a # valid spec and to remove all comments including this before submitting the spec. # To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ # Pod::Spec.new do |s| s.name = "PodName" s.version = "0.0.1" s.summary = "A short description of PodName." s.homepage = "http://github.com/xxx/PodName" s.license = { :type => "MIT", :text => <<-LICENSE Copyright © 2016年 xxx. All rights reserved. LICENSE } s.author = { "" => "" } s.source = { :git => "git@github.com:xxx/PodName.git", :tag => "0.0.1" } s.source_files = "**/*.{h,m,mm,c}" s.frameworks = "Foundation", "QuartzCore", "UIKit", "WebKit" s.libraries = "z" s.dependency 'AFNetworking' s.ios.deployment_target = '6.0' end
resource: 能夠指定資源文件,建議使用bundle以免資源文件產生衝突。
frameworks: 指定這個pod依賴的系統framework
libraries: 指定這個pod依賴的系統動態庫。注意使用的名字:好比須要引用"libz.dylib", 那麼這裏只須要寫"z"
不管原始項目的目錄結構或者group結構,默認的pod裏面的代碼都會平鋪在根目錄裏面
若是須要增長目錄層次結構,則須要使用subspec,詳細使用規範:https://guides.cocoapods.org/syntax/podspec.html#subspec
注意:SubSpecs之間不能存在相互依賴關係,只能單向依賴
$ pod lib lint --sources='git@github.com:xxx/MySpecs.git' --verbose --use-libraries --allow-warnings
sources參數能夠指定私有庫的Pod Specs庫的地址。若是可以經過,說明代碼編譯沒有問題。
$ git tag -m "first release" "0.0.1" $ git push --tags #推送tag到遠端倉庫
$ pod repo push MySpecs PodName.podspec --sources='git@github.com:xxx/MySpecs.git' --use-libraries --allow-warnings
這樣就完成了一個CocoaPods的私有庫的提交了,別人就能夠在Podfile裏面使用這個私有庫了。
你們若是還有關於CocoaPods的用法,能夠一塊兒交流。你們玩的開心~
有興趣同窗能夠關注微信公衆號奶爸碼農,不按期分享投資理財、IT相關內容: