iOS开发必备—Cocoapods安装及新建Podfile
iOS开发必备:cocoapods
一、什么是cocoapods?
CocoaPods是一个用来帮助我们管理第三方依赖库的工具。
它可以解决库与库之间的依赖关系,下载库的源代码,同时通过创建一个Xcode的workspace来将这些第三方库和我们的工程连接起来,供我们开发使用。
使用CocoaPods的目的是让我们能自动化的、集中的、直观的管理第三方开源库(比如JSONKit,AFNetWorking等)。
二、如何安装?
方法一:当然是官方的文档最权威啦,这就需要去官网看看了。
CocoaPods是Swift和Objective-C Cocoa项目的依赖管理器。 它有超过28万个图书馆,并在超过170万的应用程序中使用。 CocoaPods可以帮助您优化扩展项目。
CocoaPods是用Ruby构建的,可以使用OS X上提供的默认Ruby安装。我们建议您使用默认的ruby。
使用默认的Ruby安装可能需要在安装gem时使用sudo。 更多安装说明在指南中。
https://guides.cocoapods.org/using/getting-started.html#getting-started
这是官方的安装指南。
如果你英语和我一样不太好的话,还是用民间的传统方法吧。
方法二:小白新手安装步骤详解!!!
CocoaPods是以Ruby gem包的形式被安装的。Mac OS系统是自带Ruby环境的。
可以先在终端(Terminal)输入:sudo gem install cocoapods,如果不出错的话,那是万幸了,但是基本上都是会出现error的,因为ruby的软件源rubygems.org因为使用亚马逊的云服务,被我天朝屏蔽了,所以需要更新一下ruby的源。
保守起见首先还是先查看下当前ruby版本:
打开终端(Terminal)输入
ruby -v
2.更新Ruby源,网上的一种解决方案是将远替换成淘宝的,不过现在淘宝的 ruby 源已经不维护了,请替换成 Ruby China,
具体输入:
gem sources --removehttps://rubygems.org/ /*去掉ruby源*/
gem sources -ahttps://gems.ruby-china.org/ /*添加新的源*/
gem sources -l /*查看当前ruby源*/
3.安装:
具体输入:
sudo gem install cocoapods
4.如果你的gem太老,可能也会有问题,可以尝试用如下命令升级gem:
具体输入:
sudo gem update --system
之后的新建podfile网上都有流程啦,自己动手一下,搜索。
我要说一下我遇到的问题。
cocoapods安装及使用遇到的问题合集:
1.
安装cocoapods过程中,需要输入电脑密码,输入密码的时候,你会发现光标和平常不一样,而且你输入的时候数字会显示不出来,不用理会,直接输入完按enter/return回车键。
2.当输入“sudo gem install cocoapods”或者“sudo gem install -n /usr/local/bin cocoapods”
出现以下错误:
ERROR: While executing gem ... (Gem::DependencyError)
Unable to resolve dependencies: cocoapods requires cocoapods-core (= 1.2.0), cocoapods-downloader (< 2.0, >= 1.1.3), cocoapods-trunk (< 2.0, >= 1.1.2), molinillo (~> 0.5.5), xcodeproj (< 2.0, >= 1.4.1)
yangyangyangdeMacBook-Pro:~ yang$ sudo nvram boot-args="rootless=0"
nvram: Error setting variable - 'boot-args': (iokit/common) general error
解决方法:
输入:
rvm list known
出现错误:-bash: rvm: command not found
则继续输入:
curl -L get.rvm.io | bash -s stable
出现以下:
即表示rvm安装完成。
继续输入:
rvm list known
出现以下:
会显示出所有的ruby的版本
yangyangyangdeMacBook-Pro:~ yang$ 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[.6]
[ruby-]2.3[.3]
[ruby-]2.4[.0]
ruby-head
.
.
.
# IronRuby
ironruby[-1.1.3]
ironruby-head
继续输入:sudo gem install cocoa pods
出现以下:
yangyangyangdeMacBook-Pro:~ yang$ sudo gem install cocoa pods
Password:
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.1.gem (100%)
Successfully installed minitest-5.9.1
Fetching: concurrent-ruby-1.0.2.gem (100%)
Successfully installed concurrent-ruby-1.0.2
Fetching: activesupport-5.0.1.gem (100%)
ERROR: Error installing cocoa:
activesupport requires Ruby version >= 2.2.2.
ERROR: Error installing pods:
activesupport requires Ruby version >= 2.2.2.
继续输入:rvm install 2.2.2 /*选择安装最新的Ruby*/
出现以下:
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.12/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/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...
remote: Counting objects: 5079, done.
remote: Compressing objects: 100% (3186/3186), done.
remote: Total 5079 (delta 2798), reused 3338 (delta 1722), pack-reused 0
Receiving objects: 100% (5079/5079), 2.66 MiB | 44.00 KiB/s, done.
Resolving deltas: 100% (2798/2798), done.
From https://github.com/Homebrew/brew
...
* [new tag] 1.1.9 -> 1.1.9
HEAD is now at dfad3f3 Merge pull request #2034 from reitermarkus/spec-gpg
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 4137, done.
remote: Compressing objects: 100% (4013/4013), done.
remote: Total 4137 (delta 30), reused 433 (delta 9), pack-reused 0
Receiving objects: 100% (4137/4137), 3.31 MiB | 87.00 KiB/s, done.
Resolving deltas: 100% (30/30), done.
Tapped 4010 formulae (4,166 files, 10.4M)
==> Cleaning up /Library/Caches/Homebrew...
==> Migrating /Library/Caches/Homebrew to /Users/yang/Library/Caches/Homebrew...
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
==> Installation successful!
==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
https://git.io/brew-analytics
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://git.io/brew-docs
Installing requirements for osx.
Updating system.........
Installing required packages: autoconf, automake, libtool, pkg-config, coreutils, 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 14400 0 0:08:07 0:08:07 --:--:-- 17131
No checksum for downloaded archive, recording checksum in user configuration.
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/yang/.rvm/gems/ruby-2.2.2@global
ruby-2.2.2 - #importing gemset /Users/yang/.rvm/gemsets/global.gems.............
ruby-2.2.2 - #generating global wrappers........
ruby-2.2.2 - #gemset created /Users/yang/.rvm/gems/ruby-2.2.2
ruby-2.2.2 - #importing gemsetfile /Users/yang/.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
yang password required for 'mkdir -p /etc/openssl':
and sudo mkdir worked
继续输入:
sudo gem install cocoapods
出现以下:
最终看到了:26 gems installed
就代表最终cocoapods安装好啦
三、如何新建Podfile文件?
终端输入:
vim Podfile
点击“i”,进入文件编辑模式
输入:
platform :ios, '10.0' /*设置iOS 10的版本号*/
target 'Cloud Weather' do /*目标文件*/
pod 'AFNetworking' /*输入你要用到的第三方库,后面可以设置版本号,如果不设置就是默认最新版本*/
end
编辑完成后,然后按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq
注意:键盘输入 :后,才能输入wq。回车后发现PodTest项目总目录中多一个Podfile文件
继续在终端中输入:
pod install
如上图所示,
打开文件夹,会出现以下三个文件