iOS開發必備—Cocoapods安裝及新建Podfile

iOS開發必備:cocoapods

一、什么是cocoapods?

CocoaPods是一個用來幫助我們管理第三方依賴庫的工具。

它可以解決庫與庫之間的依賴關系,下載庫的源代碼,同時通過創建一個Xcode的workspace來將這些第三方庫和我們的工程連接起來,供我們開發使用。

使用CocoaPods的目的是讓我們能自動化的、集中的、直觀的管理第三方開源庫(比如JSONKit,AFNetWorking等)。

二、如何安裝?

方法一:當然是官方的文檔最權威啦,這就需要去官網看看了。

https://cocoapods.org/


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

這是官方的安裝指南。

如果你英語和我一樣不太好的話,還是用民間的傳統方法吧。

方法二:小白新手安裝步驟詳解?。。?/h2>

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?;剀嚭蟀l現PodTest項目總目錄中多一個Podfile文件

繼續在終端中輸入:

pod install


如上圖所示,

打開文件夾,會出現以下三個文件


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容