iOS開發(fā)必備—Cocoapods安裝及新建Podfile

iOS開發(fā)必備:cocoapods

一、什么是cocoapods倘屹?

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

它可以解決庫與庫之間的依賴關(guān)系,下載庫的源代碼务蝠,同時通過創(chuàng)建一個Xcode的workspace來將這些第三方庫和我們的工程連接起來,供我們開發(fā)使用烛缔。

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

二、如何安裝当窗?

方法一:當然是官方的文檔最權(quán)威啦够坐,這就需要去官網(wǎng)看看了寸宵。

https://cocoapods.org/


CocoaPods是Swift和Objective-C Cocoa項目的依賴管理器崖面。 它有超過28萬個圖書館,并在超過170萬的應用程序中使用梯影。 CocoaPods可以幫助您優(yōu)化擴展項目巫员。

CocoaPods是用Ruby構(gòu)建的,可以使用OS X上提供的默認Ruby安裝甲棍。我們建議您使用默認的ruby简识。

使用默認的Ruby安裝可能需要在安裝gem時使用sudo。 更多安裝說明在指南中感猛。

https://guides.cocoapods.org/using/getting-started.html#getting-started

這是官方的安裝指南七扰。

如果你英語和我一樣不太好的話,還是用民間的傳統(tǒng)方法吧陪白。

方法二:小白新手安裝步驟詳解>弊摺!咱士!

CocoaPods是以Ruby gem包的形式被安裝的立由。Mac OS系統(tǒng)是自帶Ruby環(huán)境的。

可以先在終端(Terminal)輸入:sudo gem install cocoapods序厉,如果不出錯的話锐膜,那是萬幸了,但是基本上都是會出現(xiàn)error的弛房,因為ruby的軟件源rubygems.org因為使用亞馬遜的云服務道盏,被我天朝屏蔽了,所以需要更新一下ruby的源。

保守起見首先還是先查看下當前ruby版本:

打開終端(Terminal)輸入?

ruby -v


2.更新Ruby源荷逞,網(wǎng)上的一種解決方案是將遠替換成淘寶的牺堰,不過現(xiàn)在淘寶的 ruby 源已經(jīng)不維護了,請?zhí)鎿Q成 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網(wǎng)上都有流程啦院促,自己動手一下筏养,搜索。

我要說一下我遇到的問題常拓。

cocoapods安裝及使用遇到的問題合集:

1.

安裝cocoapods過程中渐溶,需要輸入電腦密碼,輸入密碼的時候弄抬,你會發(fā)現(xiàn)光標和平常不一樣茎辐,而且你輸入的時候數(shù)字會顯示不出來,不用理會掂恕,直接輸入完按enter/return回車鍵拖陆。

2.當輸入“sudo gem install cocoapods”或者“sudo gem install -n /usr/local/bin cocoapods”

出現(xiàn)以下錯誤:

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

出現(xiàn)錯誤:-bash: rvm: command not found

則繼續(xù)輸入:

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

出現(xiàn)以下:

即表示rvm安裝完成。

繼續(xù)輸入:

rvm list known

出現(xiàn)以下:

會顯示出所有的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

繼續(xù)輸入:sudo gem install cocoa pods

出現(xiàn)以下:

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.

繼續(xù)輸入:rvm install 2.2.2? /*選擇安裝最新的Ruby*/

出現(xiàn)以下:

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


繼續(xù)輸入:

sudo gem install cocoapods

出現(xiàn)以下:


最終看到了: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钧唐∶ψ疲回車后發(fā)現(xiàn)PodTest項目總目錄中多一個Podfile文件

繼續(xù)在終端中輸入:

pod install


如上圖所示,

打開文件夾逾柿,會出現(xiàn)以下三個文件


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末缀棍,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子机错,更是在濱河造成了極大的恐慌爬范,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弱匪,死亡現(xiàn)場離奇詭異青瀑,居然都是意外死亡璧亮,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門斥难,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枝嘶,“玉大人,你說我怎么就攤上這事哑诊∪悍觯” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵镀裤,是天一觀的道長竞阐。 經(jīng)常有香客問我,道長暑劝,這世上最難降的妖魔是什么骆莹? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮担猛,結(jié)果婚禮上幕垦,老公的妹妹穿的比我還像新娘。我一直安慰自己傅联,他們只是感情好先改,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著纺且,像睡著了一般盏道。 火紅的嫁衣襯著肌膚如雪稍浆。 梳的紋絲不亂的頭發(fā)上载碌,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機與錄音衅枫,去河邊找鬼嫁艇。 笑死,一個胖子當著我的面吹牛弦撩,可吹牛的內(nèi)容都是我干的步咪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼益楼,長吁一口氣:“原來是場噩夢啊……” “哼猾漫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起感凤,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤悯周,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后陪竿,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體禽翼,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了闰挡。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锐墙。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖长酗,靈堂內(nèi)的尸體忽然破棺而出溪北,到底是詐尸還是另有隱情,我是刑警寧澤夺脾,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布刻盐,位于F島的核電站,受9級特大地震影響劳翰,放射性物質(zhì)發(fā)生泄漏敦锌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一佳簸、第九天 我趴在偏房一處隱蔽的房頂上張望乙墙。 院中可真熱鬧,春花似錦生均、人聲如沸听想。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汉买。三九已至,卻和暖如春佩脊,著一層夾襖步出監(jiān)牢的瞬間蛙粘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工威彰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留出牧,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓歇盼,卻偏偏與公主長得像舔痕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子豹缀,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容