【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)

本文主要说明CocoaPods的安装步骤、使用说明和常见的报错即解决方法。

1. CocoaPods

 1>  CocoaPods简介

  • CocoaPods是一个用来帮助我们管理第三方依赖库的工具。

  • 在开发iOS应用时,会经常使用第三方类库,比如SDWebImage、AFNetworking等等,手动的下载与添加类库非常麻烦,通过CocoaPods可以便捷的下载与管理第三方类库。

  • 使用CocoaPods的目的是让我们能自动化的、集中的、直观的管理第三方开源库。

  • 官方链接:https://cocoapods.org

  • CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境,OS X系统默认支持Ruby运行,但是Ruby的默认源使用的是rubygems.org,国内访问这个网址时,如果无法FQ,则不能下载,解决方案是将其替换成可用的Ruby,比如淘宝,具体操作详见安装。

 2>  CocoaPods安装

  • 第一步:查看自己电脑的Ruby环境,终端命令:gem sources -l

  ① 如果环境已经是taobao镜像了,此时不需要再进行环境修改

  ② 如果环境不是taobao镜像,将当前的镜像移除,终端命令:gem sources --remove https://rubygems.org/(当前的Ruby环境),接着下载taobao的镜像,终端命令:sodu gem sources -a https://ruby.taobao.org/ 

【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)_第1张图片

  • 第二步:安装CocoaPods

  ① 下载CocoaPods,终端命令:sudo gem install cocoapods (中间可能要输入管理员密码,不是明文,在你输入密码的时候不会提示)

这时候提示:

Fetching: i18n-0.7.0.gem (100%)

Successfully installed i18n-0.7.0

Fetching: thread_safe-0.3.5.gem (100%)

Successfully installed thread_safe-0.3.5

Fetching: tzinfo-1.2.2.gem (100%)

Successfully installed tzinfo-1.2.2

Fetching: minitest-5.9.0.gem (100%)

Successfully installed minitest-5.9.0

Fetching: concurrent-ruby-1.0.2.gem (100%)

Successfully installed concurrent-ruby-1.0.2

Fetching: activesupport-5.0.0.gem (100%)

ERROR:  Error installing cocoapods:

activesupport requires Ruby version >= 2.2.2.

这个地方很坑,之前安装cocoapods一直是没有这个问题的,这里提示Ruby版本要大于或等于2.2.2

于是查看ruby版本

$ruby -v

 

ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]

 

果然这里就要升级Ruby版本啦

查看目前的所有ruby版本:

 

$rvm list known

如果提示command not found 请先安装rvm

 

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

如果已安装会列出所有的ruby版本:

 

# 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[.8]

[ruby-]2.2[.4]

[ruby-]2.3[.0]

[ruby-]2.2-head

ruby-head

 

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

 

# JRuby

jruby-1.6[.8]

jruby-1.7[.23]

jruby[-9.0.5.0]

jruby-head

 

# Rubinius

rbx-1[.4.3]

rbx-2.3[.0]

rbx-2.4[.1]

rbx[-2.5.8]

rbx-head

 

# Opal

opal

 

# Minimalistic ruby implementation - ISO 30170:2012

mruby[-head]

 

# Ruby Enterprise Edition

ree-1.8.6

ree[-1.8.7][-2012.02]

 

# GoRuby

goruby

 

# Topaz

topaz

 

# MagLev

maglev[-head]

maglev-1.0.0

 

# Mac OS X Snow Leopard Or Newer

macruby-0.10

macruby-0.11

macruby[-0.12]

macruby-nightly

macruby-head

 

# IronRuby

ironruby[-1.1.3]

ironruby-head

 

这里我们安装2.2.2:

 

$rvm install 2.2.2

终端运行结果:(如果直接成功请绕过homebrew的卸载安装)

 

Searching for binary rubies, this might take some time.

Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2

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)

 

回车:

 

It appears Homebrew is already installed. If your intent is to reinstall you

should do the following before running this installer again:

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

The current contents of /usr/local are .git

Requirements installation failed with status: 1.

 

这里执行:

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

卸载home-brew

 

Warning: This script will remove:

/Library/Caches/Homebrew/

/usr/local/.git/

Are you sure you want to uninstall Homebrew? [y/N] y

==> Removing Homebrew installation...

==> Removing empty directories...

==> Homebrew uninstalled!

You may want to restore /usr/local's original permissions

  sudo chmod 0755 /usr/local

  sudo chgrp wheel /usr/local

 

再执行:

 

$ rvm install 2.2.2

提示:

 

Searching for binary rubies, this might take some time.

Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2

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)

按回车:

 

==> This script will install:

/usr/local/bin/brew

/usr/local/Library/...

/usr/local/share/doc/homebrew

/usr/local/share/man/man1/brew.1

/usr/local/share/zsh/site-functions/_brew

/usr/local/etc/bash_completion.d/brew

 

Press RETURN to continue or any other key to abort

==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew

Password:

这里需要输入电脑密码:

 

==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew

==> /usr/bin/sudo /usr/sbin/chown haha /Library/Caches/Homebrew

==> Downloading and installing Homebrew...

remote: Counting objects: 501, done.

remote: Compressing objects: 100% (445/445), done.

remote: Total 501 (delta 29), reused 360 (delta 27), pack-reused 0

Receiving objects: 100% (501/501), 787.83 KiB | 169.00 KiB/s, done.

Resolving deltas: 100% (29/29), done.

From https://github.com/Homebrew/brew

 * [new branch]      master     -> origin/master

HEAD is now at 32f7e73 download_strategy: ensure fixed commit hash length

==> Tapping homebrew/core

Cloning into '/usr/local/Library/Taps/homebrew/homebrew-core'...

remote: Counting objects: 3714, done.

remote: Compressing objects: 100% (3598/3598), done.

remote: Total 3714 (delta 14), reused 2112 (delta 6), pack-reused 0

Receiving objects: 100% (3714/3714), 2.88 MiB | 240.00 KiB/s, done.

Resolving deltas: 100% (14/14), done.

Checking connectivity... done.

Checking out files: 100% (3717/3717), done.

Tapped 3591 formulae (3,740 files, 9.0M)

==> Installation successful!

==> Next steps

Run `brew help` to get started

Further documentation: https://git.io/brew-docs

==> Homebrew has enabled anonymous aggregate user behaviour analytics

Read the analytics documentation (and how to opt-out) here:

  https://git.io/brew-analytics

Installing requirements for osx.

Updating system.....

Installing required packages: autoconf, automake, libtool, pkg-config, libyaml, readline, libksba, openssl........

Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.

Requirements installation successful.

ruby-2.2.2 - #configure

ruby-2.2.2 - #download

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100 6854k  100 6854k    0     0  61342      0  0:01:54  0:01:54 --:--:--  132k

ruby-2.2.2 - #validate archive

ruby-2.2.2 - #extract

ruby-2.2.2 - #validate binary

ruby-2.2.2 - #setup

ruby-2.2.2 - #gemset created /Users/haha/.rvm/gems/ruby-2.2.2@global

ruby-2.2.2 - #importing gemset /Users/haha/.rvm/gemsets/global.gems..............................

ruby-2.2.2 - #generating global wrappers........

ruby-2.2.2 - #gemset created /Users/haha/.rvm/gems/ruby-2.2.2

ruby-2.2.2 - #importing gemsetfile /Users/haha/.rvm/gemsets/default.gems evaluated to empty gem list

ruby-2.2.2 - #generating default wrappers........

Updating certificates in '/etc/openssl/cert.pem'.

mkdir: /etc/openssl: Permission denied

mkdir -p "/etc/openssl" failed, retrying with sudo

haha password required for 'mkdir -p /etc/openssl': 

and sudo mkdir worked

 

这样ruby2.2.2就安装好了

现在就可以安装cocoapods啦:

 

  安装过程需要稍作等待,成功后会有成功安装的提示,如图所示:

【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)_第2张图片

  ② 设置CocoaPods,终端命令:pod setup

   因为文件较大,需要等待,我们可以通过终端命令来查看进度,具体操作如下:

    <1> 重新打开一个终端,进入CocoaPods,终端命令:cd ~/.cocoapods

    <2> 查看下载进度终端命令du -sh *,这个命令只能关注当下一个时间点的进度,需要不断执行这个命令,才能随时关注下载进度

  设置成功后终端图片:

 3> CocoaPods使用

  • 先找到要查找的第三方库,终端命令:pod search + 第三方库名称

  • 在列表页面,选择三方库最新版本的命令,例如:pod 'MBProgressHUD','~>0.9.2'

【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)_第3张图片

  • 创建工程,使用 cd + 工程路径 命令进入工程(直接拖着文件到终端就可得到工程路径

  • 编辑工程的 Podfile 文件,终端命令vim Podfile

  vim 是一个强大的编辑工具,我从网上找到一篇还可以的Vim编辑器使用基础教程,有兴趣的朋友可以看看

  • 键盘输入 " i " 使 Podfile 处于编辑状态,然后将以下内容写入(高版本):

platform :ios, '8.0'
use_frameworks!
target 'MyApp(你现在项目文件夹名)' do
  pod 'MBProgressHUD', '~> 0.9.2' // 最新版本的第三方命令
end

【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)_第4张图片

  在编辑完成后,按下esc键,再按下 " : ",此时,输入 "wq" 再按回车,保存并退出。

  • 进行更新下载三方库,终端命令:pod update

                 pod install --verbose --no-repo-update (先检查一下有没有,有就不下载,没有就下载,建议使用这个)

【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)_第5张图片

  • 回到你的工程所在的目录,会发现现在多了很多文件,此时我们应该使用 .xcworkspace文件打开项目,而不在用.xcodeproj文件打开。

【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)_第6张图片

 4> CococaPods可能报错及解决方式

  • pod setup -bash: pod: command not found  

  升级10.11后使用CocoaPod出现-bash: pod: command not found

  解决办法:sudo gem install -n /usr/local/bin cocoapods

  • The dependency `MBProgressHUD (~> 1.0.1)` is not used in any concrete target.

  原因:CocoaPods升级到最新版本(包括一些高版本),Podfile文件的内容必须明确指出所用第三方库的targetName.

  报错分析:低版本和高版本写法不一样.

   低版本
    platform :ios, '8.0'
    pod 'MBProgressHUD', '~> 0.9.2'

   高版本
    platform :ios, '8.0'
    target "targetName" do
     pod 'MBProgressHUD', '~> 0.9.2'
    end

  还会有其他的一些小的容易的错误,比如:在Podfile文件中将使用中文输入法的标点符号ios写为iOS等等,大家可以根据报错自行处理

 

转载于:https://www.cnblogs.com/gfxxbk/p/5536397.html

你可能感兴趣的:(cocoapods,ruby,移动开发)