Last Updated: February 25, 2016
·
281
· nathan chang

Introduction to CocoaPods

Introduction to CocoaPods

xcode強大framework/Library 管理工具, 很多github上的強大專案也都有使用,
大部份都會在 Installation 教學使用podfile的方法
學習CocoaPods 不只build過這些專案, 更可以增加工作效率(不用include相關檔案跟 link .a或拖拉framework到專案底下)
有時候看看別人的podfile 尋找適合自己的工具也很有趣

<!-- more -->

Installation

$ sudo gem install cocoapods

官方教學

Podfile

# Networking
pod 'AFNetworking', '~> 2.0'
# Model
# Debug
pod "SPLogManager"
# UI
pod 'ECSlidingViewController', '~> 2.0.2'
# Testing

可以在自己的 xcodeproj那層新增 Podfile 內容如上
使用

pod install

替xcode project做設定 (就是以前拖拉檔案 include的繁瑣過程)
後面也可以不加上版號 在使用 pod update 即是最新版
接著

open App.xcworkspace

記得要開workspace, 很多人一開始會忘記 還是習慣開以前的 xcode project, 會奇怪為什麼build不過找不到相關檔案
最後記得source control的時候除了Podfile, Podfile.lock也要記得commit

Podspec

如果Cocoapods上找不到想要的pod
也可以自己寫 自己創造一個

$ pod spec create Peanut

會幫xcode project產生一個 .podspec
內容大致如下 針對每個欄位填寫

Pod::Spec.new do |s|
  s.name         = "SPLogManager"
  s.version      = "1.5"
  s.summary      = "A simple log manager support file logger and dynamic log level setting."

  s.description  = <<-DESC
                   SPLogManager is a simple class which manage CocoaLumberjack and NSLogger.
                   SPLogManager provide dynamic log level with type checking.
                   DESC

  s.homepage     = "https://github.com/hsin919/NSLogger-CocoaLumberjack-connector"
  s.license      = "BSD"  
  s.author             = { "hsin" => "hsin919@gmail.com" }
  s.social_media_url   = "https://twitter.com/hsin919"

  s.ios.platform   = :ios, '5.0'
  s.osx.platform   = :osx, '10.7'
  s.source       = { :git => "https://github.com/hsin919/NSLogger-CocoaLumberjack-connector.git", :tag => "1.5" }
  s.source_files  = '*.{h,m}', 'SPLogManager/*.{h,m}'
  s.requires_arc = true
  s.dependency "CocoaLumberjack"
  s.dependency "NSLogger"
end

編輯完後可以透過

$ pod spec lint Peanut.podspec

會幫忙檢查相依性 以及error/warning, 沒問題的話就會 publish出去
網路上找到很多資料會告訴你透過 pull request CocoaPods/Specs
做到merge to cocoapods的方法是舊的, pull request不會成功 必須透過上面的方法

Reference