iOS开发 Objective-C

最新的CocoaPods安装使用教程-2019-03-01

2019-03-01  本文已影响1人  落花人独立_微雨燕双飞

2019.03.01最新的CocoaPods安装方法

本教程来自对网络分享内容的整理及补充。

自己装过很多次CocoaPod,中间遇到过各种烦人的问题,也促使自己在不断的摸索中学习到了很多,总结一下,供大家学习研究,以下过程经本人新机测试,一路畅通无任何错误(请严格按照下述方法来做),中间有网络不好的可能会下载中断出错,只要从新执行一下命令就行了。

CocoaPods简介:

CocoaPods是一个用Ruby写的、负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间。

CocoaPods安装原料:

本次安装使用:

由于Xcode自带命令行工具Command_Line_Tools,所以需要先安装好Xcode,然后进行下面的安装。

其它版本的安装也可参考此教程。

CocoaPods安装:

下面就正式开始安装CocoaPods,命令中间可能有空格看不出来,建议直接复制粘贴执行。

因为Mac电脑自带Ruby环境,我们就只需打开终端开始动手。

然而又因为默认情况下我们mac系统自带的Ruby环境版本比较低(大概是2.0.0或者其他较高版本),但是现在安装CocoaPods需要2.2.2版本及以上的,所以我们不管三七二十一先直接先升级ruby。

打开终端

image

1、查看当前Ruby版本

ruby -v

显示如下信息:

ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]

2、升级Ruby环境,首先需要安装rvm(第一步要下载一些东西等两分钟左右)

curl -L get.rvm.io | bash -s stable

source ~/.bashrc

source ~/.bash_profile

显示如下信息:

192:~ mac$ curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0     85      0  0:00:02  0:00:02 --:--:--    85
100 24173  100 24173    0     0   7562      0  0:00:03  0:00:03 --:--:-- 34731
Downloading https://github.com/rvm/rvm/archive/1.29.7.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc

// 这里会停留两分钟左右,与网速有关。
……

192:~ mac$ 
192:~ mac$ source ~/.bashrc
192:~ mac$ 
192:~ mac$ source ~/.bash_profile


3、查看rvm版本

rvm -v

显示如下信息(版本号也可能是其它):

rvm 1.29.7 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

4、列出ruby可安装的版本信息

rvm list known

显示如下信息:

# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.5]
[ruby-]2.5[.3]      // ^_^ 重点在这里 重点在这里 重点在这里
                    // 建议不要选择安装最新版或者测试版。
                    // 建议安装最新版本的上一个稳定版。
[ruby-]2.6[.0]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2
……

// 测试版举例:
// [ruby-]2.6[.0-preview2]   // 测试版

5、安装一个ruby版本(这里我选择的是2.5.3版本,当然你也可以选择其它的)

安装准备:

如果你电脑没有安装

  • Xcode
  • Command Line Tools for Xcode
  • Homebrew

会自动下载安装,建议提前安装这三者。

安装Homebrew(可跳过此步骤,但不建议这样做):

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

开始安装ruby:

rvm install 2.5.3
// 注意:安装过程中需要两次按下 Enter 键,
// 第二次按下后,需要输入电脑访问密码(不可见,只管输入就行);

显示如下信息:

Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.13/x86_64/ruby-2.5.3.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user)
: 

// 翻译:
// 安装自制程序时,在“/usr/local”中按“Enter”键默认安装,
// 如果希望自定义自制酿酒安装,请键入新路径(该路径需要为用户可写)
// :

// --> 此时需要按下enter键。



Press RETURN to continue or any other key to abort
// 翻译:
// 按回车继续或任何其他键中止

// --> 此时需要按下enter键。

Password:      
// --> 此处需要输入密码,密码不可见。然后按下enter键。

按下回车后,显示如下信息(如果已经安装过Homebrew,请忽略此步):

==> Downloading and installing Homebrew...
remote: Enumerating objects: 119566, done.
Receiving objects:  32% (38266/119566), 11.76 MiB | 10.00 KiB/s    

// 会在这里停留一段时间,时长与网速有关(我等了约30分钟)。
// 下载和安装Homebrew(自制程序)。

安装完成,显示如下信息:

ruby-2.5.3 - #generating default wrappers.......
ruby-2.5.3 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.5.3 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri


// 翻译
// 安装ruby-2.5.3 - #完成
// Ruby是在没有文档的情况下构建的,构建它需要运行:rvm文档生成-ri

6、设置为默认版本

rvm use 2.5.3 --default

7、更换源

sudo gem update --system

gem sources --remove https://rubygems.org/

gem sources --add https://gems.ruby-china.com/

安装完成,显示如下信息:

------------------------------------------------------------------------------

RubyGems installed the following executables:
  /Users/mac/.rvm/rubies/ruby-2.5.3/bin/gem
  /Users/mac/.rvm/rubies/ruby-2.5.3/bin/bundle

Ruby Interactive (ri) documentation was installed. ri is kind of like man 
pages for Ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
'gem help env' for details.

RubyGems system software updated

// 翻译
// RubyGems系统软件更新

8、为了验证你的Ruby镜像是并且仅是ruby-china,执行以下命令查看

gem sources -l
*** CURRENT SOURCES ***

https://gems.ruby-china.com/

9、这时候才正式开始安装CocoaPods

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

// 需要输入电脑访问密码(不可见,只管输入就行)

10、如果安装了多个Xcode,使用下面的命令选择路径(一般需要选择最近的Xcode版本)

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

// 此处没有测试,有问题请自行百度。

11、安装本地库(推荐此方法)

pod setup

// 这条命令是将Github上的开源库都托管都安装Podspec索引安装到到本地

执行以上命令后,显示如下信息:

Setting up CocoaPods master repo
  $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
  Cloning into 'master'...
  remote: Enumerating objects: 309, done.        
  remote: Counting objects: 100% (309/309), done.        
  remote: Compressing objects: 100% (235/235), done.        
  Receiving objects:   1% (34273/2858013), 6.82 MiB | 35.00 KiB/s     

安装成功后,显示如下信息:

pod setup


Setting up CocoaPods master repo
  $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
  Cloning into 'master'...
  remote: Enumerating objects: 147, done.        
  remote: Counting objects: 100% (147/147), done.        
  remote: Compressing objects: 100% (137/137), done.        
  remote: Total 2859393 (delta 43), reused 3 (delta 3), pack-reused 2859246        
  Receiving objects: 100% (2859393/2859393), 618.83 MiB | 54.00 KiB/s, done.
  Resolving deltas: 100% (1703063/1703063), done.
  Checking out files: 100% (308708/308708), done.

CocoaPods 1.7.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.7.0.beta.1

Setup completed



如果不想等待,可以这样处理。

1)方法一

CocoaPods跳过pod setup快速安装教程

大约要下载200M的文件,偏偏下载速度出奇的慢。

本人保持在30K/S,大约2个小时,关键是没有任何进度条显示,就这样傻乎乎的,卡死在这里都不知道。

注意!

一句 pod setup 卡你2个小时,前提还是你安装没有错。如果错误,又要重新2小时。怎么能如此浪费时间呢?

前面说了,pod setup 这条命令是将Github上的开源库的Podspec索引,安装到到本地。简单说就是下载一个包,到本地,也就是master文件夹,如下图。

image

如果你同事电脑上有安装好,你其实可以从你同事电脑上拷贝过来,直接放到该路径就OK了。

如果你身边没有装好的CocoaPods的朋友,可以访问这个链接

https://github.com/CocoaPods/Specs

(github所有的第三方开源库的Podspec文件都托管在这里,这也是你pod setup所要下载的)。

选择Download ZIP,用你的浏览器或者迅雷什么的,什么下载快用什么。目前压缩包约为300M。

解压后,参照路径放置解压后的文件夹。 将master文件夹,放在 ~/.cocoapods/repos 路径即可。

~/.cocoapods/repos 这个路径是在执行 pod setup生成的。所以电脑无法前往此路径时,建议先 pod setup 下,然后关闭掉即可生成路径。

还有就是挂个VPN,下载会很快。


2)方法二

解决Cocoapods贼慢问题

https://www.jianshu.com/p/f024ca2267e3

使用SPECS 中国镜像站

http://cocoapodscn.com/

测试过此方法,但是失败了,原因未知。

使用方法

pod repo remove master

pod repo add master git://cocoapodscn.com/Specs.git
git clone git://cocoapodscn.com/Specs.git ~/.cocoapods/repos/master

pod setup
  • 方法二在 pod setup 处失败了,失败信息为:
Updating spec repo `master`
Performing a deep fetch of the `master` specs repo to improve future performance
[!] /usr/bin/git -C /Users/mac/.cocoapods/repos/master fetch --unshallow

error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

试了一些网上的方法,但都失败了。


12、查看文件下载进度(此步骤可忽略,直接看下一步。)

cd ~/.cocoapods

du -sh *

13、下载安装完成之后可执行下列命令检查是否可用(第一次使用可能要等一会)

pod search AFNetworking

14、CocoaPods的具体使用

  • 新建一个Xcode工程
  • 使用终端cd到工程目录下
pod init
  • 执行pod init之后,就可以在项目目录里看到一个Podfile文件。
open Podfile
pod 'AFNetworking'
  • 保存后退出
pod install
  • 如果安装失败,再执行一次就好了,可能是网络或者其他的原因。

到此 Perfect Success !

链接

详细的CocoaPods使用总结

https://www.jianshu.com/p/7d0ad4cde012

参考文献:

CocoaPods安装方法

CocoaPods使用总结

Homebrew介绍和使用

各种版本xcode下载

CocoaPods最新安装及跳过pod setup快速安装教程

解决Cocoapods贼慢问题

上一篇 下一篇

猜你喜欢

热点阅读