文章目录
  1. 1. CocoaPods是什么?
  2. 2. CocoaPods安装
    1. 2.1. 第一步:
    2. 2.2. 第二步:
    3. 2.3. 第三步:
  3. 3. 升级CocoaPod
  4. 4. CocoaPods使用
  5. 5. 为自己的项目创建 podspec 文件
  6. 6. 使用私有的 pods

CocoaPods是什么?

  1. 说这么一个现场,如果要引用第三方类库是不是要把他拖进项目引用或者copy,如果第三方类库更新了,你是不是又要重新应用更新啊?答案是肯定的。

  2. 另外一种情况,如果项目过大,且项目中有很多相对独立的功能,分工到不同人的手中维护升级开发,最后整合项目(其实可把他们看作第三方类库),是不是也会遇到copy或者引用的问题。

以上两点会导致,遇到更改就要换代码换文件,如果你的项目就一个,可以很轻松换掉,如果是10个 ,100个….估计就该抱怨了,cocoapods一个链接就搞定了,可以很好提高效率,同时有利于分工协作。

今天把cocoapods安装过程总结分享给大家。

CocoaPods安装

第一步:

安装CocoaPods之前,首先要在本地安装好Ruby环境。(ruby安装 https://ruby-china.org/wiki/install_ruby_guide

安装ruby过程如下:

首先确保已经安装Xcode的 Command Line Tools

然后再打开终端输入:

1
2
3
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
sudo chown -R $(whoami):admin /usr/local
brew install ruby

第二步:

当执行成功后,在终端输入:

1
sudo gem install -n /usr/local/bin cocoapods

发现没有反映,别急换个方法:

如果发现报:Could not find a valid gem ‘cocoapods’ (>= 0) in any repository 别急,这个问题说明是你的gem版本过旧了 (没有报错此步骤可以跳过)

请尽可能用比较新的 RubyGems 版本,建议 2.6.x 以上。

在终端先输入:

1
2
gem update --system # 有可能需要翻墙
gem -v

重新打开终端输入:

1
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/

会返回:

1
2
https://gems.ruby-china.org/ added to sources
https://rubygems.org/ removed from sources

检测一下在终端输入:

1
gem sources -l

得到如下:

1
2
*** CURRENT SOURCES ***
https://gems.ruby-china.org/

确保只有 gems.ruby-china.org

第三步:

再次输入:

1
sudo gem install -n /usr/local/bin cocoapods

等待几十秒会返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Fetching: multi_json-1.10.1.gem (100%)
Successfully installed multi_json-1.10.1
Fetching: activesupport-3.2.19.gem (100%)
Successfully installed activesupport-3.2.19
Fetching: nap-0.8.0.gem (100%)
Successfully installed nap-0.8.0
Fetching: json_pure-1.8.1.gem (100%)
Successfully installed json_pure-1.8.1
Fetching: fuzzy_match-2.0.4.gem (100%)
Successfully installed fuzzy_match-2.0.4
Fetching: cocoapods-core-0.33.1.gem (100%)
Successfully installed cocoapods-core-0.33.1
Fetching: claide-0.6.1.gem (100%)
Successfully installed claide-0.6.1
Fetching: colored-1.2.gem (100%)
Successfully installed colored-1.2
Fetching: xcodeproj-0.17.0.gem (100%)
Building native extensions. This could take a while...
[!] You are using the prebuilt binary version of the xcodeproj gem.
Successfully installed xcodeproj-0.17.0
Fetching: cocoapods-downloader-0.6.1.gem (100%)
Successfully installed cocoapods-downloader-0.6.1
Fetching: cocoapods-plugins-0.2.0.gem (100%)
Successfully installed cocoapods-plugins-0.2.0
Fetching: cocoapods-try-0.3.0.gem (100%)
Successfully installed cocoapods-try-0.3.0
Fetching: netrc-0.7.7.gem (100%)
Successfully installed netrc-0.7.7
Fetching: cocoapods-trunk-0.1.4.gem (100%)
Successfully installed cocoapods-trunk-0.1.4
Fetching: escape-0.0.4.gem (100%)
Successfully installed escape-0.0.4
Fetching: open4-1.3.4.gem (100%)
Successfully installed open4-1.3.4
Fetching: cocoapods-0.33.1.gem (100%)
CHANGELOG:
## 0.33.1
##### Bug Fixes
* Fix `pod spec lint` for `json` podspecs.
[Fabio Pelosin][irrationalfab]
[#2157](https://github.com/CocoaPods/CocoaPods/issues/2157)
* Fixed downloader issues related to `json` podspecs.
[Fabio Pelosin][irrationalfab]
[#2158](https://github.com/CocoaPods/CocoaPods/issues/2158)
* Fixed `--no-ansi` flag in help banners.
[Fabio Pelosin][irrationalfab]
[#34](https://github.com/CocoaPods/CLAide/issues/34)
Successfully installed cocoapods-0.33.1
Parsing documentation for multi_json-1.10.1
Installing ri documentation for multi_json-1.10.1
Parsing documentation for activesupport-3.2.19
unable to convert "\x80" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping
Installing ri documentation for activesupport-3.2.19
Parsing documentation for nap-0.8.0
Installing ri documentation for nap-0.8.0
Parsing documentation for json_pure-1.8.1
Installing ri documentation for json_pure-1.8.1
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for cocoapods-core-0.33.1
Installing ri documentation for cocoapods-core-0.33.1
Parsing documentation for claide-0.6.1
Installing ri documentation for claide-0.6.1
Parsing documentation for colored-1.2
Installing ri documentation for colored-1.2
Parsing documentation for xcodeproj-0.17.0
Installing ri documentation for xcodeproj-0.17.0
Parsing documentation for cocoapods-downloader-0.6.1
Installing ri documentation for cocoapods-downloader-0.6.1
Parsing documentation for cocoapods-plugins-0.2.0
Installing ri documentation for cocoapods-plugins-0.2.0
Parsing documentation for cocoapods-try-0.3.0
Installing ri documentation for cocoapods-try-0.3.0
Parsing documentation for netrc-0.7.7
Installing ri documentation for netrc-0.7.7
Parsing documentation for cocoapods-trunk-0.1.4
Installing ri documentation for cocoapods-trunk-0.1.4
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for open4-1.3.4
Installing ri documentation for open4-1.3.4
Parsing documentation for cocoapods-0.33.1
Installing ri documentation for cocoapods-0.33.1
17 gems installed

在安装进程结束的时候,执行命令:

1
pod setup

如果没有报错,就说明一切安装就成功了!

如果出现下面的错误:

1
2
3
4
error: Your local changes to the following files would be overwritten by merge:
CocoaPods-version.yml
Specs/BCMagicMoveTransition/1.0.2/BCMagicMoveTransition.podspec.json
Please commit your changes or stash them before you merge.

执行下面的命令:

1
2
cd ~/.cocoapods/repos/master
git reset HEAD --hard

升级CocoaPod

升级很简单,再次执行安装命令即可:

1
sudo gem install -n /usr/local/bin cocoapods

需要注意的是,如果安装的时候使用了sudo,升级的时候一样需要使用该关键字,不然升级完了以后又会出现路径不匹配问题。

CocoaPods使用

使用很简单如在终端输入:

1
pod search AFNetworking

结果返回:Setting up CocoaPods master repo

嚓,知道吗?我等了足足半个小时。所以请耐心等待.

半小时之后返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Setup completed (read-only access)
-> AFNetworking (2.3.1)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 2.3.1'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0, 2.1.0, 2.0.3, 2.0.2, 2.0.1,
2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4, 1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0,
1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2, 1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0,
0.5.1 [master repo]
- Sub specs:
- AFNetworking/Serialization (2.3.1)
- AFNetworking/Security (2.3.1)
- AFNetworking/Reachability (2.3.1)
- AFNetworking/NSURLConnection (2.3.1)
- AFNetworking/NSURLSession (2.3.1)
- AFNetworking/UIKit (2.3.1)
-> AFNetworking+AutoRetry (0.0.3)
Auto Retries for AFNetworking requests
pod 'AFNetworking+AutoRetry', '~> 0.0.3'
- Homepage: https://github.com/shaioz/AFNetworking-AutoRetry
- Source: https://github.com/shaioz/AFNetworking-AutoRetry.git
- Versions: 0.0.3, 0.0.2, 0.0.1 [master repo]
-> AFNetworking+streaming (0.5)
A very quick extension to AFNetworking that adds stream based parsing
pod 'AFNetworking+streaming', '~> 0.5'
- Homepage: https://github.com/deanWombourne/AFNetworking-streaming
- Source: https://github.com/deanWombourne/AFNetworking-streaming.git
- Versions: 0.5, 0.4, 0.3, 0.2, 0.1 [master repo]
- Sub specs:
- AFNetworking+streaming/Core (0.5)
- AFNetworking+streaming/Json (0.5)
-> AFNetworking-MUResponseSerializer (1.0.0)
Automatically serialize JSON response to your object model. It also works with
NSManagedObjects.
pod 'AFNetworking-MUResponseSerializer', '~> 1.0.0'
- Homepage: https://github.com/Ulianko/AFNetworking-MUResponseSerializer
- Source: https://github.com/Ulianko/AFNetworking-MUResponseSerializer.git
- Versions: 1.0.0 [master repo]
-> AFNetworking-RACExtensions (0.1.4)
AFNetworking-RACExtensions is a delightful extension to the AFNetworking classes for iOS
and Mac OS X.
pod 'AFNetworking-RACExtensions', '~> 0.1.4'
- Homepage: https://github.com/CodaFi/AFNetworking-RACExtensions
- Source: https://github.com/CodaFi/AFNetworking-RACExtensions.git
- Versions: 0.1.4, 0.1.2, 0.1.1, 0.0.1 [master repo]
-> AFNetworking-ReactiveCocoa (0.0.2)
Make AFNetworking reactive.
pod 'AFNetworking-ReactiveCocoa', '~> 0.0.2'
- Homepage: https://github.com/uasi/AFNetworking-ReactiveCocoa
- Source: https://github.com/uasi/AFNetworking-ReactiveCocoa.git
- Versions: 0.0.2, 0.0.1 [master repo]
-> AFNetworking-Synchronous (0.2.0)
Synchronous requests for AFNetworking
pod 'AFNetworking-Synchronous', '~> 0.2.0'
- Homepage: https://github.com/paulmelnikow/AFNetworking-Synchronous
- Source: https://github.com/paulmelnikow/AFNetworking-Synchronous.git
- Versions: 0.2.0, 0.1.0 [master repo]
-> AFNetworking2-RACExtensions (0.0.1)
AFNetworking-RACExtensions is a delightful extension to the AFNetworking classes for iOS
and Mac OS X.
pod 'AFNetworking2-RACExtensions', '~> 0.0.1'
- Homepage: https://github.com/knshiro/AFNetworking-RACExtensions
- Source: https://github.com/knshiro/AFNetworking-RACExtensions.git
- Versions: 0.0.1 [master repo]
-> CollectionUtils-AFNetworking (1.0.1)
CollectionUtils additions for AFNetworking.
pod 'CollectionUtils-AFNetworking', '~> 1.0.1'
- Homepage: https://github.com/kishikawakatsumi/CollectionUtils
- Source: https://github.com/kishikawakatsumi/CollectionUtils.git
- Versions: 1.0.1, 1.0.0 [master repo]
-> CollectionUtils-AFNetworking-1.3 (1.0.1)
CollectionUtils additions for AFNetworking 1.3.x.
pod 'CollectionUtils-AFNetworking-1.3', '~> 1.0.1'
- Homepage: https://github.com/kishikawakatsumi/CollectionUtils
- Source: https://github.com/kishikawakatsumi/CollectionUtils.git
- Versions: 1.0.1, 1.0.0 [master repo]

为自己的项目创建 podspec 文件

我们可以为自己的开源项目创建 podspec 文件,首先通过如下命令初始化一个podspec文件:

1
pod spec create your_pod_spec_name

该命令执行之后,CocoaPods 会生成一个名为 your_pod_spec_name.podspec 的文件,然后我们修改其中的相关内容即可。

使用私有的 pods

我们可以直接指定某一个依赖的 podspec ,这样就可以使用公司内部的私有库。该方案有利于使企业内部的公共项目支持 CocoaPods。如下是一个示例:

1
pod 'MyCommon', :podspec => 'https://us.com/common/myCommon.podspec'