基于CocoaPods的iOS項目模塊化實踐

JYLGar 8年前發布 | 38K 次閱讀 CocoaPods iOS開發 移動開發

來自: http://www.cnblogs.com/wdsunny/p/5187528.html

什么是CocoaPods?

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.org

在CocoaPods出現之前,iOS項目中引用第三方庫的方式是非常原始的,要么是把源代碼拷貝到主工程中,要么是通過靜態庫引入.a文件,然后還要修改一系列的build settings。后續的第三方庫的升級也是個枯燥乏味的事情,總之如果你的iOS項目目前還是這樣管理第三方庫,那么你們還處在石器時代。

CocoaPods通過集中式的管理,可以非常有效的管理第三方庫,甚至可以用于大型項目的模塊化管理,非常優雅高效的解決iOS項目中的依賴管理。

安裝CocoaPods

CocoaPods是一個Ruby Gem,因為直接訪問RubyGem速度非常慢,建議先替換成淘寶鏡像

$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/

安裝CocoaPods

$ sudo gem install cocoapods

管理第三方庫

創建Podfile

在項目根目錄下創建Podfile,下面是一個Podfile的例子 (詳情可以參考http://guides.cocoapods.org/syntax/podfile.html#podfile):

platform :ios, '9.0'

target "MyApp" do pod 'ObjectiveSugar', '~> 0.5'

target "MyAppTests" do pod 'OCMock', '~> 2.0.1' end end</pre>

platform: 可以指定平臺的信息和deployment target的版本

target: 可以根據不同的target來引入不同的pod

pod: 引入依賴庫

pod 'SSZipArchive'  -- 引入最新版本

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

安裝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打開項目。

使用CocoaPods管理私有庫

大型項目模塊化管理

隨著iOS APP越來越復雜,功能越來越多,對于iOS項目的工程化要求也越來越高了,對于復雜的APP一般都需要對項目進行模塊化管理。

模塊化有幾個方式:

1. 目錄結構管理:這是最原始的方式,僅僅通過目錄結構實現代碼層次的清晰化。但本質上并沒有解決代碼之間的依賴混亂的情況,模塊化劃分也非常不清晰。

2. 子工程:通過子工程可以實現代碼依賴管理和模塊化,但是需要引入復雜的設置,不利于管理。

3. 靜態庫:將依賴代碼打包成為靜態庫.a,不過由于不能看到源碼,調試不方便。

自從有了CocoaPods,可以使用它來管理私有庫,從而實現了代碼模塊化管理。例如下圖所示:

CocoaPods私有庫

1. 創建私有的Specs git庫

例如在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中。

2.在私有庫項目中創建podspec文件

在私有庫項目中的根目錄,創建對應的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之間不能存在相互依賴關系,只能單向依賴

3. 驗證私有庫的合法性

$ pod lib lint --sources='git@github.com:xxx/MySpecs.git' --verbose --use-libraries --allow-warnings

sources參數可以指定私有庫的Pod Specs庫的地址。如果能夠通過,說明代碼編譯沒有問題。

4. 提交私有庫的版本信息

$ git tag -m "first release" "0.0.1"
$ git push --tags     #推送tag到遠端倉庫

5.向Spec Repo提交podspec

$ pod repo push MySpecs PodName.podspec --sources='git@github.com:xxx/MySpecs.git' --use-libraries --allow-warnings

這樣就完成了一個CocoaPods的私有庫的提交了,別人就可以在Podfile里面使用這個私有庫了。

大家如果還有關于CocoaPods的用法,可以一起交流。大家玩的開心~

 本文由用戶 JYLGar 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!