CocoaPods 使用總結

CocoaPods簡介

CocoaPods 是專門爲 iOS 工程提供第三方依賴庫的管理工具,通過 CocoaPods,我們可以更方便地管理每個第三方庫的版本,而且不需要我們做太多的配置,就可以直觀、集中和自動化地管理我們項目的第三方庫。

CocoaPods 將所有依賴的庫都放在一個名爲 Pods 的項目下,然後讓主項目依賴 Pods 項目。然後,我們編碼工作都從主項目轉移到 Pods 項目。Pods 項目最終會編譯爲一個 libPod-項目名.a 靜態庫,主項目依賴於這個靜態庫。

對於資源文件,CocoaPods 提供了一個名爲 Pods-resources.shbash 腳本,該腳本在每次項目編譯的時候都會執行,將第三方庫的各種資源文件複製到目標目錄中。CocoaPods 通過一個名爲 Pods.xcconfig 的文件來在編譯時設置所有的依賴和參數。CocoaPods 是用 Ruby 寫的,並由若干個 Ruby 包 (gems) 構成的。在解析整合過程中,最重要的幾個 gems 分別是: CocoaPods/CocoaPodsCocoaPods/CoreCocoaPods/Xcodeproj

CocoaPod的核心組件

  • CocoaPods/CocoaPod
    這是一個面向用戶的組件,每當執行一個 pod 命令時,這個組件都將被激活。該組件包括了所有使用 CocoaPods 涉及到的功能,並且還能通過調用所有其它的 gems 來執行任務。
  • CocoaPods/Core
    Core 組件提供支持與 CocoaPods 相關文件的處理,文件主要是 Podfilepodspecs
  • Podfile
    Podfile 是一個文件,用於定義項目所需要使用的第三方庫。該文件支持高度定製,你可以根據個人喜好對其做出定製。更多相關信息,請查閱 Podfile 指南。
  • Podspec
    .podspec 也是一個文件,該文件描述了一個庫是怎樣被添加到工程中的。它支持的功能有:列出源文件、framework、編譯選項和某個庫所需要的依賴等。
  • CocoaPods/Xcodeproj
    這個 gem 組件負責所有工程文件的整合。它能夠創建並修改 .xcodeproj.xcworkspace 文件。它也可以作爲單獨的一個 gem 包使用。如果你想要寫一個腳本來方便地修改工程文件,那麼可以使用這個 gem

私有庫開發流程

一般公司都會搭建自己的 git 服務器,在實戰項目中,經常使用 Cocoapods 管理自己的私有庫。

  • git 倉庫至少需要兩個,一個用於管理私有庫對應版本的 podspec 文件,一個用於存放私有庫的源文件。
  • 查看現有的 podspec 源地址:
cd ~/.cocoapods/repos

創建一個私有的 podspec 主要包括如下幾步:

  • 創建一個私有的 Spec Repo,用於管理私有庫對應版本的 podspec 文件;
  • 創建 pod 私有庫所需要的項目工程文件,並上傳到私有庫;
  • 創建 pod 所對應的 podspec 文件,並進行驗證/測試;
  • 向私有的 Spec Repo 中提交 podspec
  • 使用 pod 庫;

創建一個私有的 Spec Repo

  • 在自己公司的 git 服務器上創建 SpecsRepo 倉庫;
  • 將私有 SpecsRepo 關聯到本地;
pod repo add IMXSpecsRepo http://192.168.120.32/app/sisi-iOS/IMXSpecsRepo.git

查看 SpecsRepo 是否創建成功

cd ~/.cocoapods/repos

創建 pod 私有庫所需要的項目工程

命令行創建

pod lib create 倉庫名字 --template-url=http://192.168.120.32/app/sisi-iOS/IMXPodTemplte

手動創建

手動創建 Xcode 工程項目,創建 podspec 文件

pod spec create XXXX.podspec

修改 podspec 文件

Pod::Spec.new do |s|
  s.name             = 'AKCBrowser'
  s.version          = '0.0.1'
  s.summary          = 'A short description of AKCBrowser.'

  # This description is used to generate tags and improve search results.
  #   * Think: What does it do? Why did you write it? What is the focus?
  #   * Try to keep it short, snappy and to the point.
  #   * Write the description between the DESC delimiters below.
  #   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
  TODO: Add long description of the pod here.
  DESC

  s.homepage         = 'http://192.168.120.32/app/akc-framework-ios/AKCBrowser.git'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'sunjinshuai' => '[email protected]' }
  s.source           = { :git => 'http://192.168.120.32/app/akc-framework-ios/AKCBrowser.git', :tag => s.version.to_s }

  s.static_framework      = true
  s.requires_arc = true
  s.frameworks = 'UIKit', 'Foundation'
  s.ios.deployment_target = '8.0'
  s.xcconfig = {
    'GCC_PREPROCESSOR_DEFINITIONS' => 'MAS_SHORTHAND_GLOBALS=1'
  }
  s.resource_bundles = {
    'Browser' => ['AKCBrowser/Assets/Browser/*.*']
  }
  s.source_files = 'AKCBrowser/Classes/**/*'

  s.dependency 'SDWebImage','4.2.3'
  s.dependency 'YYImage'
  s.dependency 'AKCFoundation'
  s.dependency 'AFNetworking/Reachability'
  s.dependency 'SVProgressHUD'
  s.dependency 'AKCLog'

end

podspec 語法簡介

s.name:私有庫的名稱,`pod search`搜索的關鍵詞,注意這裏一定要和`.podspec`的名稱一樣,否則報錯。
s.version:私有庫的版本。
s.ios.deployment_target:支持的pod最低版本。
s.summary:私有庫簡介。
s.description:私有庫詳細介紹。
s.homepage:私有庫在GitHub上的地址。
s.license:開源協議。
s.author:作者。
s.social_media_url:社交網址,你的podspec發佈成功後會@你
s.source:私有庫在GitHub上的地址和版本號。
s.source_files:私有庫對外共享的.h和.m文件。
s.requires_arc:是否支持ARC。

更多語法查看(http://guides.cocoapods.org/making/specs-and-specs-repo.html)

上傳項目工程的源文件

git add -A
git commit -m "first commit"
git remote add origin http://192.168.120.32/app/akc-framework-ios/AKCBrowser.git
git push -u origin master
git tag -m "first release" 0.0.1
git push --tags     #推送tag到遠端倉庫

驗證 podspec 文件

pod lib lint --allow-warnings --verbose
pod lib lint --use-libraries --allow-warnings   // 使用靜態庫後的用法
pod lib lint --sources=http://192.168.120.32/app/sisi-iOS/IMXSpecsRepo.git

當看到 AKCBrowser passed validation。表示驗證通過。

向 Spec Repo 中提交 podspec 文件

# 如果依賴其他私有源 同樣需要加上 --sources 參數
pod repo push IMXSpecsRepo AKCFoundation.podspec --allow-warnings
pod repo push IMXSpecsRepo AKCProtobuf.podspec --allow-warnings --use-libraries  // 使用靜態庫後的用法

pod repo push IMXSpecsRepo AKCFoundation.podspec --allow-warnings --sources='http://192.168.120.32/ddcang/iOSGiftBox/SpecsRepo.git'

多 target 時 Podfile 該如何寫

platform :ios, '8.0'

def commonPods #通用pods集
    pod 'AFNetworking', '~> 2.0'
    pod 'Masonry'
end

def appOnlyPods #app專用pods集
    pod 'MBProgressHUD'
end

def extensionPods #擴展專用pods集
    pod 'GTSDKExtension'
end

target :TestCocoaPods do
    commonPods
    appOnlyPods

    target :TestCocoaPodsTests do
    inherit! :search_paths
    # Pods for testing
    end

    target :TestCocoaPodsUITests do
        inherit! :search_paths
        # Pods for testing
    end
end

target :SecondTarget do
    commonPods
end

如何忽略Pods警告

Podfile 中對應的 target 或分組下加上關鍵字 inhibit_all_warnings即可。

如何直接引用第三方庫中的頭文件

在用 CocoaPods 集成第三方庫之後,默認情況下,我們需要使用類似 #import <XXX/YYY.h>的方式引入第三方庫的頭文件。

可以在 Build Settings -> User Header Search Paths 中添加 ${SRCROOT} 並設置成 recursive,這樣我們就可以直接使用 #impot "YYY.h"這種方式了。

pod install 和 pod update 的選擇

pod install:

按照官方文檔所說,pod install在第一次檢索集成第三方以及每一次在 Podfile 中新增、更改或刪除 pod 的時候使用。每一次執行 pod install命令,它都會下載安裝新的 pod,並且會把每一個安裝的 pod 的版本信息寫入 Podfile.lock 文件。Podfile.lock 文件跟蹤每一個安裝的 pod 的版本並且上鎖。每一次執行 pod install命令,只解決還沒有在 Podfile.lock 中列出的依賴:對於已在 Podfile.lock 中列出的 pod,會下載指定的版本,不會檢查是否有新版本。對於沒有在 Podfile.lock 中列出的 pod,它會搜索並安裝 Podfile 中指定的版本。

pod update:

直接執行 pod update命令會檢查安裝 Podfile 中列出的所有 pod 的最新版本。只有當你想要更新 pod 庫的版本時才使用 pod update;它不管 Podfile.lock是否存在,都會讀取 Podfile 文件的最新版本,下載好之後,重新生成 Podfile.lock文件。

兩者的區別:

  • pod install命令來安裝新的 pod,每次在 Podfile 中新增和刪除 pod 都使用 pod install命令。

  • Podfile 中添加新的 pod 後應該用 pod install命令,而不是 pod update命令。通過 pod install命令安裝新的 pod 而不用擔心在同一進程中修改已有的 pod

  • pod update命令僅用在更新指定 pod 到指定版本或者更新所有 pod

如果想更新指定的 pod 倉庫,可以使用

pod update XXX –no-repo-update

如果想安裝新添加的庫

pod install –no-repo-update

當需要在 CocoaPods 中刪除一個我們不要的庫時 可以在 Podfile 中直接刪除相關庫;

pod update --no-repo-update 會在刪除相關庫時 更新其他庫版本
pod update XXX --no-repo-update 只會刪除相關庫 和下方一致
pod install --no-repo-update 只會刪除相關庫

關於版本指定約束

一般我們在使用 cocoapods 導入第三方庫前都會生成一個 podfile 文件,文件中記錄着我們要導入的第三方庫以及對應的版本信息,比如:

pod 'SDWebImage', '~> 4.3.2'

cocoapods 導入 SDWbImage,版本號 4.3.2 和版本號處於 4.3.2-4.4 之間的,不包括 4.4 和更高版本。

pod 'SDWebImage' --- 不指定版本,表示希望使用最新版本
pod 'SDWebImage', '4.3.2' --- 指定明確版本,表示只想要這個版本
邏輯關係
'> 0.1' --- 版本號大於0.1的
'>= 0.1' --- 版本0.1和版本號大於0.1的
'< 0.1' --- 版本號小於0.1的
'<= 0.1' --- 版本號0.1和版本號小於0.1的
最優匹配
'~> 0.1.2' --- 版本0.1.2和版本號處於0.1.2-0.2之間的,不包括0.2和更高版本
'~> 0.1' --- 版本0.1和版本號處於0.1-1.0之間的,不包括1.0和更高版本
'~> 0' --- 版本0和更高,和沒設沒啥區

tag 改動原則:

  • 增加 api,修改第三位;
  • 修改 api,修改第二位;
  • 大版本變更修改第一位;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章