CocoaPods操作手冊

CocoaPods操作手冊

本文檔介紹了啥?

為什么要使用CocoaPods?

如何安裝CocoaPods?

Podfile語法

Podspec語法

命令行相關

常見問題

簡介

CocoaPods是類庫管理工具。它有超過2.9萬個庫,超過180萬個app在使用。工程所需要的依賴庫都在Podfile里面。CocoaPods可以解決庫之間的依賴,獲取源碼,并把它們與Xcode工程鏈接到一塊,去編譯你的工程。

下載和安裝

1.首先安裝Ruby環境

  • 下載安裝Xcode

    它集成了Unix環境需要的開發包

  • 安裝RVM,命令如下:

    $ curl -L https://get.rvm.io | bash -s stable  #下載安裝
    $ source ~/.rvm/scripts/rvm #載入RVM環境
    $ rvm -v #運行下命令,查看版本號,以檢查是否安裝正確
    
  • 用RVM安裝Ruby環境:(ruby版本需要到2.0以上,不然pod install/update不能生成xcworkspace文件)

    $ rvm list known #列出已知的ruby版本
    $ rvm install 2.2 #以2.2版本為例進行安裝
    

    等待一段時間,安裝完成已經,可查詢已經安裝的ruby,命令如下:

    $ rvm list
    

2.修改鏡像源

由于gem resources官方鏡像源地址https://rubygems.org/被墻,需要修改,依次輸入以下命令即可:

$ gem sources --remove https://rubygems.org/ #移除原有的鏡像源
$ gem sources -a http://ruby.taobao.org/ #添加淘寶的鏡像源

可驗證Ruby鏡像源是否替換成功,命令如下:

$ gem sources -l

正常輸出如下:

*** CURRENT SOURCES ***

https://ruby.taobao.org

3.安裝cocoapods

命令如下:

$ sudo gem install cocoapods

4.更新cocoapods

通常情況下,輸入$sudo gem install cocoapods即可。若想安裝beta版本或者未正式發布的版本,可輸入以下命令:

$ sudo gem install cocoapods --pre

如果再安裝不了,可嘗試:

$ sudo gem install -n /usr/local/bin cocoapods --pre

5.卸載cocoapods

$sudo gem uninstall cocoapods

使用Cocoapods

1.創建Podfile

在工程所在目錄下創建Podfile文件,添加依賴(首先要保證所添加的庫是有效的):

platform :ios, '8.0' #平臺,支持osx, ios, tvos, watchos
use_frameworks! #使用frameworks代替靜態庫

target 'MyApp' do #指定一個目標,便于鏈接
  pod 'AFNetworking', '~> 2.7.3'
end

運行pod install,打開MyApp.xcworkspace即可

在工程里導入依賴,示例如下:

#import <AFNetworking/AFNetworking.h>
#import "AFNetworking.h"

如果未使用use_frameworks!,導入依賴示例如下:

#import <AFNetworking/AFNetworking.h>
#import "AFNetworking.h"
#import <AFNetworking.h>

也可以自己指定工程,指定工作空間,如:

workspace 'Weather' #引號之間是workspace所在的路徑

project 'weather/weather.xcodeproj' #cocoapods1.0以后使用,之前使用xcodeproj

target 'weather' do
  pod 'AFNetworking', '~> 3.0'
end

多個目標使用相同的pod,使用abstract_target:

# There are no targets called "Shows" in any Xcode projects
abstract_target 'Shows' do
  pod 'ShowsKit'
  pod 'Fabric'

  # Has its own copy of ShowsKit + ShowWebAuth
  target 'ShowsiOS' do
    pod 'ShowWebAuth'
  end

  # Has its own copy of ShowsKit + ShowTVAuth
  target 'ShowsTV' do
    pod 'ShowTVAuth'
  end
end

由于Podfile暗含一個abstract target, 因此上面也可以簡單的寫成

pod 'ShowsKit'
pod 'Fabric'

# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
  pod 'ShowWebAuth'
end

# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
  pod 'ShowTVAuth'
end

指定pod版本

使用最新版本:

pod 'AFNetworking'

指定某個版本:

pod 'AFNetworking', '2.7.3'

可以使用邏輯表達式,不指定固定版本:

  • '> 0.1' 大于0.1的版本
  • '>= 0.1' 0.1版本和大于0.1的版本
  • '< 0.1' 小于0.1的版本
  • '<= 0.1' 0.1版本和小于0.1的版本

為了可以進行邏輯操作,CocoaPods有個優化的操作符 ~>:

  • '~> 0.1.2' 版本0.1.2和0.2以下的版本,不包括0.2及更高的版本,相當于>=0.1.2 && < 0.2.0
  • '~> 0.1' 相當于>=0.1 && < 0.2
  • '~> 0' 版本0和更高版本,基于上等于沒限制

指定自定義版本

使用倉庫的master分支,最新版本

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'

使用倉庫的dev分支

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'

使用倉庫的tag

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'

或者是他用某一次提交

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'

當開發一個庫的時候,使用:path,指向本地路徑

pod 'Alamofire', :path => '~/Documents/Alamofire'

其它語法

1.隱藏某個庫的警告

pod 'SSZipArchive', :inhibit_warnings => true

2.編譯配置

pod 'PonyDebugger', :configurations => ['Debug', 'Beta']

3.不指定子倉庫

pod 'AFNetworking'

會拉全部子倉庫, Podfile.lock文件內容如下:

PODS:
  - AFNetworking (3.1.0):
    - AFNetworking/NSURLSession (= 3.1.0)
    - AFNetworking/Reachability (= 3.1.0)
    - AFNetworking/Security (= 3.1.0)
    - AFNetworking/Serialization (= 3.1.0)
    - AFNetworking/UIKit (= 3.1.0)
  - AFNetworking/NSURLSession (3.1.0):
    - AFNetworking/Reachability
    - AFNetworking/Security
    - AFNetworking/Serialization
  - AFNetworking/Reachability (3.1.0)
  - AFNetworking/Security (3.1.0)
  - AFNetworking/Serialization (3.1.0)
  - AFNetworking/UIKit (3.1.0):
    - AFNetworking/NSURLSession

DEPENDENCIES:
  - AFNetworking

SPEC CHECKSUMS:
  AFNetworking: 5e0e199f73d8626b11e79750991f5d173d1f8b67

PODFILE CHECKSUM: 62ce9f109426c7163a93c5cb33e94ec6900149fd

COCOAPODS: 1.2.0

4.指定子倉庫,語法如下:

pod 'AFNetworking/Serialization'
pod 'AFNetworking/Security'
pod 'AFNetworking/NSURLSession'

pod 'AFNetworking', :subspecs => ['Serialization', 'Security', 'NSURLSession'] #此處以Serialization, Security, NSURLSession為例

其它未指定的子倉庫會被移除,Podfile.lock文件內容如下:

PODS:
  - AFNetworking/NSURLSession (3.1.0):
    - AFNetworking/Reachability
    - AFNetworking/Security
    - AFNetworking/Serialization
  - AFNetworking/Reachability (3.1.0)
  - AFNetworking/Security (3.1.0)
  - AFNetworking/Serialization (3.1.0)

DEPENDENCIES:
  - AFNetworking/NSURLSession
  - AFNetworking/Security
  - AFNetworking/Serialization

SPEC CHECKSUMS:
  AFNetworking: 5e0e199f73d8626b11e79750991f5d173d1f8b67

PODFILE CHECKSUM: 72d9296c101235bacf706bf9428c01cb550c46c9

COCOAPODS: 1.2.0

5.指定podspec

pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec'

2.背后發生了啥

  • 創建或更新workspace
  • 把主工程添加到workspace
  • 把cocoapods靜態庫工程添加到workspace,如果存在就跳過
  • 添加libPods.a,targets => build phases => link with libraries
  • 添加cocoapods的xcode configurations 文件(.xcconfig)到主工程
  • 改變應用的target configurations
  • 向copy resources from any pods添加一個build phase,比如script build phase
    • shell: /bin/sh
    • script: ${SRCROOT}/Pods/PodsResources.sh

3.pod install vs pod update

pod install

  • 第一次安裝時會創建Podfile.lock文件,把版本鎖定
  • 當存在Podfile.lock時,完全按照Podfile.lock里面列的,下載同樣的版本,并不檢測新版本
  • 當從Podfile里面添加或移除pod時,應使用pod install

pod update

  • 更改pod到新版本,并更新Podfile.lock對應的版本依賴
  • 用法:pod update PODNAME 更新某個pod庫;pod update 更新所有pod庫。

pod outdated

  • 檢測Podfile.lock中所列庫對應的新版本
target 'MyApp' do #指定一個目標,便于鏈接
  pod 'AFNetworking', '~> 2.7.3' #2.7.3至2.8版本,不包括2.8版本
  pod 'FBSDKCoreKit', '~> 4.9' #4.9至5.0版本,不包括5.0版本
  pod 'Objection', '0.9' #固定到0.9版本??
end

4.Pods文件夾要納入版本控制嗎?

納入版本控制的好處:

  • 即使沒有安裝Cocoapods也可以編譯運行,不需要pod install,不需要連網
  • Pod庫永遠是可用的,即使遠程庫宕機,或消失了
  • Pod庫和遠程倉庫里是一致的

忽略Pods文件夾的好處:

  • 遠程倉庫占用空間小
  • 只要所有的pod庫都可用,CocoaPods會重新創建同樣的安裝過程
  • 即使使用不同的pod版本,不用擔心沖突

無論是否控制Pods文件夾,Podfile和Podfile.lock都應該納入版本控制。

5.Podfile.lock是啥

PODS:
  - AFNetworking (3.1.0):
    - AFNetworking/NSURLSession (= 3.1.0)
    - AFNetworking/Reachability (= 3.1.0)
    - AFNetworking/Security (= 3.1.0)
    - AFNetworking/Serialization (= 3.1.0)
    - AFNetworking/UIKit (= 3.1.0)
  - AFNetworking/NSURLSession (3.1.0):
    - AFNetworking/Reachability
    - AFNetworking/Security
    - AFNetworking/Serialization
  - AFNetworking/Reachability (3.1.0)
  - AFNetworking/Security (3.1.0)
  - AFNetworking/Serialization (3.1.0)
  - AFNetworking/UIKit (3.1.0):
    - AFNetworking/NSURLSession
  - AKNumericFormatter (0.0.2)
.....

DEPENDENCIES:
  - AKNumericFormatter (~> 0.0.2)
  - BlocksKit (= 2.2.3)
  - DACircularProgress (~> 2.3.1)
....

EXTERNAL SOURCES:
  MWPhotoBrowser:
    :git: http://121.40.102.80:8888/libs/mwphotobrowser.git
    :tag: 1.0.1
....

CHECKOUT OPTIONS:
  MWPhotoBrowser:
    :git: http://121.40.102.80:8888/libs/mwphotobrowser.git
    :tag: 1.0.1
....

SPEC CHECKSUMS:
  AFNetworking: 5e0e199f73d8626b11e79750991f5d173d1f8b67
  AKNumericFormatter: de969ab13c504dca7aa119b558a934ccdbd1e376
.....

PODFILE CHECKSUM: 19a24fb4caf70d68f18a1643582ca013dee97168

COCOAPODS: 1.2.0

6.Pods和Submodules

git submodules和CocoaPods功能類似。submodules鏈接到一個特定的commit, CocoaPod捆綁到一個版本化的開發版本。

制作一個CocoaPod

創建pod

1.創建

pod lib create MyLib #這里以MyLib為例

2.使用tree命令查看目錄樹(可通過命令brew install tree安裝)

$ tree MyLib -L 2 

  MyLib
  ├── .travis.yml #travis-ci(開源持續集成構建項目,類似jenkins, GO)的一個安裝文件
  ├── _Pods.xcproject #pods工程的鏈接,支持Carthage(另外一個第三方庫管理工具)
  ├── Example #demo
  │   ├── MyLib
  │   ├── MyLib.xcodeproj
  │   ├── MyLib.xcworkspace
  │   ├── Podfile
  │   ├── Podfile.lock
  │   ├── Pods
  │   └── Tests
  ├── LICENSE #默認是MIT聲明
  ├── MyLib.podspec #庫對應的Podspec
  ├── Pod #放庫文件的地方
  │   ├── Assets
  │   └── Classes
  │     └── RemoveMe.[swift/m]
  └── README.md #markdown格式的默認readme

注:當向Pod/Assets或Pod/Classes文件夾添加、或移除文件、或更新podspec文件,你應當運行pod install或pod update

3.部署

部署前應該檢查下Podspec文件的語言是否符合規則

pod lib lint #不需要連接網絡

pod spec lint #檢測外部的倉庫和相關的tag

4.開發引入

pod 'Name', :path => '~/code/Pods/'

Podspec語法

specification規范

描述一個Pod庫版本,包括去哪里獲取獲取源文件,使用什么文件,應用什么編譯設置,還有一些元數據,包括名稱,版本,描述等

Pod::Spec.new do |spec|
  spec.name         = 'Reachability' #名稱
  spec.version      = '3.1.0' #版本
  spec.license      = { :type => 'BSD' } #聲明
  spec.homepage     = 'https://github.com/tonymillion/Reachability' #主頁
  spec.authors      = { 'Tony Million' => 'tonymillion@gmail.com' } #作者
  spec.summary      = 'ARC and GCD Compatible Reachability Class for iOS and OS X.' #pod的描述,最多140個字
  spec.source       = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => s.version.to_s } #檢索的庫的位置,注意這里使用的是s.version.to_s,可以動態跟spec.version保持一致
  spec.source_files = 'Reachability.{h,m}'
  spec.framework    = 'SystemConfiguration'
end

詳細描述

name 名稱,必需

spec.name = 'AFNetworking'

version 版本,必需

spec.version = '0.0.1'

cocoapods_version 規范支持的cocoapods版本號

spec.cocoapods_version = '>= 0.36'

author 作者,必需

spec.author = 'Darth Vader'
spec.authors = 'Darth Vader', 'Wookiee'
spec.authors = { 'Darth Vader' => 'darthvader@darkside.com',
                 'Wookiee'     => 'wookiee@aggrrttaaggrrt.com' }

license 聲明,必需

spec.license = 'MIT'
spec.license = { :type => 'MIT', :file => 'MIT-LICENSE.txt' }
spec.license = { :type => 'MIT', :text => <<-LICENSE
                   Copyright 2012
                   Permission is granted to...
                 LICENSE
               }

支持的鍵有,:type, :file, :text

homepage 主頁,必需

spec.homepage = 'http://www.example.com'

source 源,必需

指定一個git源,并指定tag

spec.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git',
                :tag => spec.version.to_s }

指定以v開頭的tag,和子模塊

spec.source = { :git => 'https://github.com/typhoon-framework/Typhoon.git',
                :tag => "v#{spec.version}", :submodules => true }

使用svn,并指定tag

spec.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => '4.8.8' }

使用Mercurial(一種輕量級分布式版本控制系統),并指定revision版本

spec.source = { :hg => 'https://bitbucket.org/dcutting/hyperbek', :revision => "#{s.version}" }

使用http下載一個壓縮包,支持zip, tgz, bz2, txz和tar

spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip' }

使用http去下載一個文件,并使用hash值去驗證下載,支持sha1和sha256

spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip',
                :sha1 => '7e21857fe11a511f472cfd7cfa2d979bd7ab7d96' }

支持的鍵有

:git => :tag, :branch, :commit, :submodules

:svn => :folder, :tag, :revision

:hg => :revision

:http => :flatten, :type, :sha256, :sha1

:path

summary 摘要,必需

最多140個字的pod描述

spec.summary = 'Computes the meaning of life.'

description 描述

比摘要更詳細的描述

spec.description = <<-DESC
                     Computes the meaning of life.
                     Features:
                     1. Is self aware
                     ...
                     42. Likes candies.
                   DESC

screenshots 屏幕截圖

url圖片列表,用于面向UI的庫,CocoaPods推薦使用gif格式

spec.screenshot  = 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png'
spec.screenshots = [ 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png',
                     'http://dl.dropbox.com/u/378729/MBProgressHUD/2.png' ]

deprecated 棄用

spec.deprecated = true

platform 平臺

spec.platform = :osx, '10.8'
spec.platform = :ios

deployment_target 部署目標

spec.ios.deployment_target = '6.0'
spec.osx.deployment_target = '10.8'

相對于plateform鍵而言,它可以指定不同的部署目標

dependency 依賴

spec.dependency 'AFNetworking', '~> 1.0'
spec.dependency 'RestKit/CoreData', '~> 0.20.0'
spec.ios.dependency 'MBProgressHUD', '~> 0.5'

注:podspec中的dependency依賴只能引入Cocopods官方的庫,或者在工程的podfile中指定的私有repo中的庫(這個創建私有repo以后會講到),不支持以其它方式引入,如:git, :path等等

requires_arc 需要使用arc

默認是true

spec.requires_arc = false
spec.requires_arc = 'Classes/Arc'
spec.requires_arc = ['Classes/*ARC.m', 'Classes/ARC.mm']

framework 框架,多平臺

spec.ios.framework = 'CFNetwork'
spec.frameworks = 'QuartzCore', 'CoreData'

libraries 庫,多平臺

spec.ios.library = 'xml2'
spec.libraries = 'xml2', 'z'

如在支付寶官方iOS接入文檔中(https://docs.open.alipay.com/204/105295/),需要引入的庫如下圖

3ebefcabdf8062f717dbb5d866ba7cfb.jpg

那么,在podspec文件中,對frameworks及libraries的寫法應該是

  spec.framework = 'SystemConfiguration', 'CoreTelephony', 'QuartzCore', 'CoreText', 'CoreGraphics', 'UIKit', 'Foundation', 'CFNetwork', 'CoreMotion' //不帶后綴
  spec.ios.libraries = 'c++', 'z' //不帶tbd后綴及lib前綴
  spec.vendored_frameworks = 'MyFile/AlipaySDK.framework' //此為自定義的framework

compiler_flags 編譯標志位,多平臺

spec.compiler_flags = '-DOS_OBJECT_USE_OBJC=0', '-Wno-format'

pod_target_xcconfig,多平臺

spec.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }

prefix_header_contents,多平臺

不推薦使用,會污染別的庫或主工程的prefix header

spec.prefix_header_contents = '#import <UIKit/UIKit.h>'
spec.prefix_header_contents = '#import <UIKit/UIKit.h>', '#import <Foundation/Foundation.h>'

prefix_header_file,多平臺

不推薦使用,會污染別的庫或主工程的prefix header

spec.prefix_header_file = 'iphone/include/prefix.pch'

module_name, 模塊名

如不設置,默認是specification的名稱

spec.module_name = 'Three20'

header_dir,多平臺

存放頭文件的目錄

spec.header_dir = 'Three20Core'

header_mappings_dir,多平臺

存放頭文件目錄的路徑

spec.header_mappings_dir = 'src/include'

文件類型

模式1:*(檢測文件名)

  • * 匹配所有文件
  • c* 匹配以c開頭的文件
  • *c 匹配以c結尾的文件
  • *c* 匹配以包括c的文件

模式2:**

  • 遞歸匹配目錄

模式3:?

  • 匹配任何一個字符

模式4:[set]

  • 匹配任何在set中的字符

模式5:{p, q}

  • 匹配p,或q

模式6:\

  • 遺棄下一個元字符
"JSONKit.?"    #=> ["JSONKit.h", "JSONKit.m"]
"*.[a-z][a-z]" #=> ["CHANGELOG.md", "README.md"]
"*.[^m]*"      #=> ["JSONKit.h"]
"*.{h,m}"      #=> ["JSONKit.h", "JSONKit.m"]
"*"            #=> ["CHANGELOG.md", "JSONKit.h", "JSONKit.m", "README.md"]

source_files 源文件,多平臺

spec.source_files = 'Classes/**/*.{h,m}'
spec.source_files = 'Classes/**/*.{h,m}', 'More_Classes/**/*.{h,m}'

public_header_files 公共頭文件,多平臺

spec.public_header_files = 'Headers/Public/*.h'

private_header_files 私有頭文件,多平臺

未在public headers中,不應該暴露給主工程,

spec.private_header_files = 'Headers/Private/*.h'

vendored_frameworks, 多平臺

spec.ios.vendored_frameworks = 'Frameworks/MyFramework.framework'
spec.vendored_frameworks = 'MyFramework.framework', 'TheirFramework.framework'

vendored_libraries,多平臺

spec.ios.vendored_library = 'Libraries/libProj4.a'
spec.vendored_libraries = 'libProj4.a', 'libJavaScriptCore.a'

resource_bundles,多平臺

強烈推薦使用,可避免命名沖突,可測試

spec.ios.resource_bundle = { 'MapBox' => 'MapView/Map/Resources/*.png' }
spec.resource_bundles = {
    'MapBox' => ['MapView/Map/Resources/*.png'],
    'OtherResources' => ['MapView/Map/OtherResources/*.png']
  }

resources 資源文件,多平臺

推薦使用resource bundles

spec.resource = 'Resources/HockeySDK.bundle'
spec.resources = ['Images/*.png', 'Sounds/*']

exclude_files,多平臺

spec.ios.exclude_files = 'Classes/osx'
spec.exclude_files = 'Classes/**/unused.{h,m}'

preserve_paths,多平臺

下載后不被移除的文件,默認的是,cocoapods會移除其它類型的文件

spec.preserve_path = 'IMPORTANT.txt'
spec.preserve_paths = 'Frameworks/*.framework'

module_map,多平臺???

spec.module_map = 'source/module.modulemap'

子庫

subspec

subspec 'Twitter' do |sp|
  sp.source_files = 'Classes/Twitter'
end

subspec 'Pinboard' do |sp|
  sp.source_files = 'Classes/Pinboard'
end

使用方法

pod 'ShareKit/Twitter',  '2.0'
pod 'ShareKit/Pinboard', '2.0'

default_subspecs

spec.default_subspec = 'Core'
spec.default_subspecs = 'Core', 'UI'

命令行相關

安裝

$ touch Podfile
$ vi Podfile
$ pod install
$ open *.xcworkspace

pod init

生成默認的podfile文件

pod init XCODEPROJ #xcodeproj為工程名,project會在podfile里面指定

舉例:pod init weather.xcodeproj

pod install

下載podfile中所有的依賴,并在./Pods中創建一個xcode pods library工程

選項:

—repo-update 在安裝前更新pod倉庫,先運行pod repo update

—no-repo-update 在安裝前不更新pod倉庫

—project-directory=/project/dir/ 工程根目錄路徑

pod update

pod update [POD_NAMES ...]#如果不指定pod_names,將更新所有pod

使用:pod update AFNetworking CommontTools

選項:

—repo-update 在安裝前更新pod倉庫,先運行pod repo update

—no-repo-update 在安裝前不更新pod倉庫

—project-directory=/project/dir/ 工程根目錄路徑

pod outdated

pod outdated

列舉podfile.lock中過時的pod,只檢測spec倉庫,不包括本地或外地的源

選項:

—repo-update 在安裝前更新pod倉庫,先運行pod repo update

—no-repo-update 在安裝前不更新pod倉庫

—project-directory=/project/dir/ 工程根目錄路徑

pod deintegrate

v1.0.0.beta.1之后可用

pod deintegrate [XCODE_PROJECT] #如果不指定,將會從當前目錄尋找xcode工程

從cocoapods分離主工程,從主工程移除所有cocoapods的痕跡

選項:

—project-directory=/project/dir/ 工程根目錄路徑

pod env

顯示pod環境

pod search

v0.0.2以上可用

pod search QUERY #查找pod

query為要查詢的內容,不分大小寫,將會查找name, summary, description或作者。

選項:

—regex 把query當作正則表達式

—simple 只按名稱搜索pod庫

—ios/osx/watchos/tvos 搜索適用某個平臺的庫

—no-pager 搜索結果不要分頁

—web 在cocopods.org上搜索

—顯示額外的統計,如github上的watchers和forks

pod list

列出所有可用的pod

pod list

選項

—update 在列舉前進行更新,即運行pod repo update

—顯示額外的統計,如github上的watchers和forks

pod try

v0.29.0之后可用

pod try NAME|URL

按pod名或pod的git url下載,安裝依賴,并打開demo工程

選項:

—podspec_name=[name] 指定podspec文件的名稱,如提供url地址,可在后面跟上該選項

—no-repo-update 在安裝前不更新pod倉庫

pod spec create

pod spec create [NAME|https://github.com/USER/REPO]

在當前文件夾下創建一個podspec,如果github url是可用的話,將會預填一部分內容到spec文件中,

如:pod spec create https://github.com/hengyizhangcn/EditView

pod spec lint

pod spec lint [NAME.podspec|DIRECTORY|http://PATH/NAME.podspec ...]

驗證podspec文件,參數可以不提供,驗證當前文件夾

選項:

—quick 跳過需要下載和編譯的步驟

—allow-warnings 允許警告

—subspec=NAME 只驗證給定的subspec

—no-subspecs 路過驗證subspec

—no-clean 將構建目錄保留完好以供檢查

—fail-fast 在第一個平臺或subspec失敗時即停止

—use-libraries 使用靜態庫安裝spec

—private 只檢測私有spec

—swift-version=VERSION 指定swift版本

--sources=https://github.com/artsy/Specs,master 下拉的依賴庫中的源(默認是https://github.com/CocoaPods/Specs.git)。 多個源之間用逗號分隔

pod spec cat

pod spec cat [QUERY]

打印名稱匹配query的podspec內容

選項:

—regex 把query當作正則表達式

—show-all 選自給定podspec的所有版本

pod spec which

pod spec which [QUERY]

打印名稱匹配query的podspec路徑

選項:

—regex 把query當作正則表達式

—show-all 選自給定podspec的所有版本

pod spec edit

pod spec edit [QUERY]

打開名稱匹配query的podspec以供編輯

選項:

—regex 把query當作正則表達式

—show-all 選自給定podspec的所有版本

pod lib create

pod lib create NAME

選項:

—template-url=URL git repo的URL包含一個兼容性的模板

pod lib lint

驗證庫使用的文件

—quick 跳過需要下載和編譯的步驟

—allow-warnings 允許警告

—subspec=NAME 只驗證給定的subspec

—no-subspecs 路過驗證subspec

—no-clean 將構建目錄保留完好以供檢查

—fail-fast 在第一個平臺或subspec失敗時即停止

—use-libraries 使用靜態庫安裝spec

—private 只檢測私有spec

—swift-version=VERSION 指定swift版本

--sources=https://github.com/artsy/Specs,master 下拉的依賴庫中的源(默認是https://github.com/CocoaPods/Specs.git)。 多個源之間用逗號分隔

pod cache list

pod cache list [NAME] #name可不提供

顯示pods緩存的內容,以YAML樹的形式輸出

選項:

—short 只打印相對路徑

pod cache clean

pod cache clean [NAME]

選項:

—all 清除所有緩存的pods

注:要么指定一個pod名,要么帶上—all參數

Trunk

Repos

IPC

Plugins

經常碰到的問題

1.連接超時

[!] /usr/bin/git clone https://github.com/jpush/jpush-ios-sdk-pod.git /var/folders/8w/j6x5f1290zq_g0q8b0dp7fzm0000gn/T/d20170227-15744-1kyd22x --template= --single-branch --depth 1 --branch 2.2.0.1
Cloning into '/var/folders/8w/j6x5f1290zq_g0q8b0dp7fzm0000gn/T/d20170227-15744-1kyd22x'...
error: RPC failed; curl 56 SSLRead() return error -36
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: unpack-objects failed

2.找不到pod

Unable to find a pod with name, author, summary, or description matching.

刪除索引即可:rm ~/Library/Caches/CocoaPods/search_index.json

3.當存在多個Xcode版本

[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master-1`.
You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`.

在Xcode->Preference->locations里面選擇command tools line

4.target未添加

The dependency  is not used in any concrete target.

當pod升級后需要在pod file添加target,如target ‘MyProject’ do *** end

5.域權限控制問題

Uncategoried
Command /usr/sbin/chown failed with exit code 1
chown REMAINTECH\Domain Users: illegal group name

原因有以下幾種:
1)Charles抓包工具,打開了ssl選項,導致登錄驗證不正確
各種試過刪除pods、重新install,不行之后(記住,千萬不要亂改xcode配置以免出現未知問題):
2)把網絡從無線切換到有線之后就可以了
可能是公司網絡域控導致的,之前安裝時從有線網絡下載,切換網絡之后導致問題

在添加target或者刪除target時,需要先運行pod deintegrate, 不然Build Phases中的Embed Pods Frameworks的shell路徑會改變

6.找不到cocoaPods,一般是安裝新的rvm導致的

/Library/Ruby/Site/2.3.0/rubygems.rb:271:in `find_spec_for_exe': can't find gem cocoapods (>= 0.a) (Gem::GemNotFoundException)
    from /Library/Ruby/Site/2.3.0/rubygems.rb:299:in `activate_bin_path'
    from /usr/local/bin/pod:23:in `<main>'

卸載并重新安裝cocoapods

sudo gem uninstall cocoapods
gem install cocoapods

參考

https://github.com/CocoaPods/CocoaPods/wiki

http://guides.cocoapods.org/using/pod-install-vs-update.html#introduction

如何使用Carthage管理iOS依賴庫

http://www.lxweimin.com/p/5ccde5f22a17

http://guides.cocoapods.org/making/specs-and-specs-repo.html

http://guides.cocoapods.org/syntax/podspec.html#header_mappings_dir

http://guides.cocoapods.org/making/private-cocoapods.html

http://guides.cocoapods.org/syntax/podspec.html#group_multi_platform_support

http://guides.cocoapods.org/terminal/commands.html#commands








最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 項目組件化、平臺化是技術公司的共同目標,越來越多的技術公司推崇使用pod管理第三方庫以及私有組件,一方面使項目架構...
    swu_luo閱讀 22,217評論 0 39
  • 一. CocoaPods的介紹 什么是CocoaPods?CocoaPods是一個負責管理iOS項目中第三方開源庫...
    輝712閱讀 3,993評論 0 7
  • CocoaPods 是什么? CocoaPods 是一個負責管理 iOS 項目中第三方開源庫的工具。CocoaPo...
    朝洋閱讀 25,741評論 3 50
  • Ruby 安裝 要安裝cocospods 首先需要安裝ruby,可以先安裝xcode,再安裝macport ,最后...
    山天大畜閱讀 1,887評論 0 1
  • 天剛亮便匆忙起床了。心中還帶著一絲絲不滿地穿上衣服,坐起來呆呆地望著墻。 天啊!再讓我睡會吧。但某種力量在拉扯...
    林呀林閱讀 208評論 0 0