前戏
首先查看一下ruby和gem的版本,可能需要更新。我们可以使用RVM,但是官方建议尽量使用mac自带的ruby
CocoaPods is built with Ruby and it will be installable with the default Ruby available on macOS. You can use a Ruby Version manager, however we recommend that you use the standard Ruby available on macOS unless you know what you're doing.
- 查看ruby版本
ruby -v
- 查看gem版本
gem -v
- 查看ruby源
gem source -l
如果ruby源是默认源或者淘宝源,那么更新源
- 移除
gem source -r https://rubygems.org/ 或者 https://ruby.taobao.org/
- 添加
gem source -a https://gems.ruby-china.org/
- 更新gem
sudo gem update --system
- 安装CocoaPods
sudo gem install -n /usr/local/bin cocoapods
- 查看CocoaPods版本
pod --version
- 部署podspec文件的本地仓库
创建目录~/.cocoapods/repos
,并且克隆一个公开的spec-repo,仓库名为master,地址是https://github.com/CocoaPods/Specs.git
pod setup
- 列出本地仓库
pod repo list
master
- Type: git (master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/YJ/.cocoapods/repos/master
1 repo
如果master仓库源是默认源,那么可以考虑更新源,但是这里不建议更新源,github源只是第一次执行pod setup
命令花费大量时间,相比之下,国内镜像问题多多,大致是这样的
oschina源https://git.oschina.net/akuandev/Specs.git
已经不存在了
gitcafe源https://gitcafe.com/akuandev/Specs.git
2016.03.02迁移到coding
coding源https://git.coding.net/CocoaPods/Specs.git
2017.03.16迁移到aliyun和gitclub
aliyun源https://code.aliyun.com/Magi/CocoaPods.git
gitclub源https://gitclub.cn/CocoaPods/Specs.git
- 移除
pod repo remove 仓库名
- 添加
pod repo add 仓库名 URL
添加命令可能报错
[!] To setup the master specs repo, please run `pod setup`.
直接克隆
git clone https://code.aliyun.com/Magi/CocoaPods.git 或者 https://gitclub.cn/CocoaPods/Specs.git ~/.cocoapods/repos/master
- 更新本地仓库
通过省略仓库名,更新所有仓库
pod repo update
更新master仓库
pod repo update master
创建podfile
-
创建项目
-
cd进入项目根目录
-
通过
pod init
命令创建podfile文件
CocoaPods提供pod init
命令创建一个带有默认配置的podfile文件

Dependencies
- pod
通过省略pod的版本,指定最新版本
pod 'AFNetworking'
通过数字,指定某个特定版本
pod 'AFNetworking', '2.5.0'
除此之外,还可以使用运算符
= 0.1 0.1的版本
> 0.1 任何大于0.1的版本
>= 0.1 任何大于等于0.1的版本
< 0.1 任何小于0.1的版本
<= 0.1 任何小于等于0.1的版本
~> 0.1 0.1的版本到0.2的版本,包括0.1,不包括0.2,总是匹配最新版本
- target
podfile的target和Xcode的target是一一对应关系
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'CocoaPodsDemoWithOC' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
use_frameworks!
# Pods for CocoaPodsDemoWithOC
pod 'AFNetworking', '2.5.0'
pod 'SDWebImage', '3.7.5'
end
target 'CocoaPodsDemoWithSwift' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for CocoaPodsDemoWithSwift
pod 'AFNetworking', '2.5.0'
pod 'SDWebImage', '3.7.5'
end
使用inherit!
设置继承模式,:complete
继承所有父类行为,:none
不继承父类行为,:search_paths
只继承父类的search paths
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'CocoaPodsDemoWithOC' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
use_frameworks!
# Pods for CocoaPodsDemoWithOC
pod 'AFNetworking', '2.5.0'
pod 'SDWebImage', '3.7.5'
target 'CocoaPodsDemoWithOCTests' do
inherit! :search_paths
# Pods for testing
pod 'MJRefresh', '3.0.0'
end
end
target 'CocoaPodsDemoWithSwift' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for CocoaPodsDemoWithSwift
pod 'AFNetworking', '2.5.0'
pod 'SDWebImage', '3.7.5'
target 'CocoaPodsDemoWithSwiftTests' do
inherit! :search_paths
# Pods for testing
pod 'MJRefresh', '3.0.0'
end
end
使用abstract_target定义多个target继承依赖
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
abstract_target 'CocoaPodsDemo' do
use_frameworks!
pod 'AFNetworking', '2.5.0'
pod 'SDWebImage', '3.7.5'
target 'CocoaPodsDemoWithOC'
target 'CocoaPodsDemoWithSwift'
end
Target configuration
- platform
指定工程Pods的Deployment Target,确保Deployment Target大于等于库要求的,否则报错
[!] Unable to satisfy the following requirements:
- `Alamofire (= 4.0.0)` required by `Podfile`
Specs satisfying the `Alamofire (= 4.0.0)` dependency were found, but they required a higher minimum deployment target.
平台的name,:osx
for OS X,:ios
for iOS,:tvos
for tvOS,or :watchos
for watchOS
如果没有指定平台,那么CocoaPods自动分配平台为ios,Deployment Target为9.0
[!] Automatically assigning platform ios with version 9.0 on target CocoaPodsDemoWithOC because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
如果没有指定Deployment Target,那么对于不同平台,CocoaPods提供不同默认值,4.3 for iOS, 10.6 for OS X, 9.0 for tvOS and 2.0 for watchOS
-
inhibit_all_warnings!
关于禁止警告,可以看我的上一篇文章Xcode消除编译器警告 -
use_frameworks!
使用framework代替library,Swift项目必须使用use_frameworks!
安装更新依赖
-
通过
pod install
或者pod update
命令安装或者更新 -
pod install
可选选项 :--repo-update
执行pod install
之前强制执行pod repo update
-
pod update
可选选项 :--no-repo-update
执行pod update
之前跳过pod repo update
pod install还是pod update
当第一次使用CocoaPods部署项目时或者当添加删除更新一个pod时,可以使用pod install
- 每次执行
pod install
,对于每个pod已经安装的版本,它会写在Podfile.lock文件,这个文件记录每个pod已经安装的版本并且锁定那些版本,Podfile.lock文件大概长这样的
PODS:
- AFNetworking (2.5.0):
- AFNetworking/NSURLConnection (= 2.5.0)
- AFNetworking/NSURLSession (= 2.5.0)
- AFNetworking/Reachability (= 2.5.0)
- AFNetworking/Security (= 2.5.0)
- AFNetworking/Serialization (= 2.5.0)
- AFNetworking/UIKit (= 2.5.0)
- AFNetworking/NSURLConnection (2.5.0):
- AFNetworking/Reachability
- AFNetworking/Security
- AFNetworking/Serialization
- AFNetworking/NSURLSession (2.5.0):
- AFNetworking/Reachability
- AFNetworking/Security
- AFNetworking/Serialization
- AFNetworking/Reachability (2.5.0)
- AFNetworking/Security (2.5.0)
- AFNetworking/Serialization (2.5.0)
- AFNetworking/UIKit (2.5.0):
- AFNetworking/NSURLConnection
- AFNetworking/NSURLSession
- MJRefresh (3.0.0)
- SDWebImage (3.7.5):
- SDWebImage/Core (= 3.7.5)
- SDWebImage/Core (3.7.5)
DEPENDENCIES:
- AFNetworking (= 2.5.0)
- MJRefresh (= 3.0.0)
- SDWebImage (= 3.7.5)
SPEC CHECKSUMS:
AFNetworking: 96ac9bf3eda33582701cb1fcc5b896aa1e20311e
MJRefresh: 72e41ae6c0e9331da3e19cf7e9286a2b1b62ff5f
SDWebImage: 69c6303e3348fba97e03f65d65d4fbc26740f461
PODFILE CHECKSUM: d86820d289392f6ad6293e733113a16c4751d05b
COCOAPODS: 1.2.1
- 对于Podfile.lock文件列出的pods,它会下载Podfile.lock文件列出的版本,不会尝试检查一个新的可用版本。对于Podfile.lock文件没有列出的pods。它会寻找一个版本匹配podfile文件的描述
当执行pod update
时,它会更新每个pod到最新版本(只要匹配podfile文件的描述就行),不考虑Podfile.lock文件
例如,podfile文件的描述
pod 'AFNetworking'
执行pod install
,由于此时没有Podfile.lock文件,安装当时最新的2.5.0版本并且记录在Podfile.lock文件。一段时间之后,再次执行pod install
,由于有Podfile.lock文件,版本还是2.5.0,如果执行pod update
,那么更新此时最新版本3.0.0
总结Podfile.lock文件,pod install,pod update
- 对于Podfile.lock文件官方建议加入版本控制
- 对于podfile文件指定某个特定版本,这样
pod install
和pod update
几乎没区别 - 对于podfile文件没有指定某个特定版本,知道什么时候
pod install
什么时候pod update
References :
Podfile Syntax Reference
Command-line Reference
网友评论