iOS技术程序员程序猿阵线联盟-汇总各类技术干货

Mac下CocoaPods安装与使用

2017-12-17  本文已影响329人  WenBo丨星空灬

前言

在开发项目的时候,难免会导入一些三方开源库,CocoaPods是OS X管理三方开源库的工具,用这个工具,我们可以轻松集中管理、更新三方开源库。下面开始介绍CocoaPods安装与使用吧。

介绍内容目录

一、安装RVM

curl -L get.rvm.io | bash -s stable 
rvm -v
rvm get stable
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[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head
//安装2.4.1
rvm install 2.4.1

安装如果遇到如下错误:


屏幕快照 2017-12-17 下午1.59.45.png

安装Command Line Tools即可

xcode-select --install
rvm list

输出结果如下:

rvm rubies
=* ruby-2.4.1 [ x86_64 ]
# => - current
# =* - current && default
#  * - default
rvm current
rvm use 2.4.1 --default
rvm remove 2.2.2

如果提示权限不足,同理加上sudo

sudo rvm remove 2.2.2

二、升级RubyGems

gem update --system

若果是最新,则输出:

Latest version currently installed. Aborting.
sudo gem update --system
gem sources --remove https://rubygems.org/

gem sources -a https://gems.ruby-china.org/
gem source -l

输出结果:

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

三、CocoaPods安装

sudo gem install -n /usr/local/bin cocoapods
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
pod setup

执行上面的命令过后,会出现卡主不动,这个是时候是在下载,通常会等很久。这是后可以通过cmmand+n新创一个终端窗口,然后cd ~/.cocoapods/到该文件下,执行du -sh *查看大小:

1015M   repos
pod --version
sudo gem install -n /usr/local/bin cocoapods

四、CocoaPods使用

工程导入三方库
cd 工程路径
pod init

这时工程就会生成一个podfile


屏幕快照 2017-12-17 下午5.39.24.png
vim podfile

进入之后按i进入编辑模式,添加三方开源库如:pod 'AFNetworking'(也可指定版本pod 'AFNetworking', '~> 3.1.0'),然后输入:wq回车保存。

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
pod 'AFNetworking'
target 'test' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for test

end
~                                                                               
~                                                                               
~                                                                                                                                                        
:wq
pod install
或
pod install --no-repo-update

好了,到此CocoaPods的安装与使用都介绍完毕。


屏幕快照 2017-12-17 下午5.47.41.png
更新三方库
//更新所有可更新的三方库
pod update
cd ~/.cocoapods
ls
cd repos
ls
cd master
ls
ls -a
git pull
pod update Masonry
pod update --verbose --no-repo-update
移除导入过的三方库
pod install
移除工程中CocoaPods

问题解决

1、执行gem update --system报证书错误,在网上找了很久也没有找到解决方法,后来还是找到了,方法是忽略证书验证。

步骤:
前往~/.gemrc,打开文件,并添加:ssl_verify_mode: 0

---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://gems.ruby-china.org/
:update_sources: true
:verbose: true
:ssl_verify_mode: 0

cmmand+s保存,然后在执行sudo gem update --system,更新成功。

2、Unable to require openssl, install OpenSSL and rebuild ruby
//如果没有安装openssl,则用honebrew安装
brew install openssl

//重装rvm并关联openssl
rvm reinstall 2.4.0 --with-openssl-dir=`brew --prefix openssl`

如果安装了2.4.0版本则重新安装,没有安装则安装,安装成功之后,就能sudo gem update --system正常更新了。

总结

CocoaPods安装与使用就介绍到这里了,如果在以后CocoaPods安装使用工程中遇到问题,如果找到了解决方案,我也会贴出来。

参考文章

上一篇下一篇

猜你喜欢

热点阅读