新建一個工程cocoapodsTest
1雁佳、在終端中輸入cd,將cocoapodsTest工程直接拖到終端中,如下:
cd /Users/admin/Desktop/cocoapodsTest
2押蚤、當(dāng)前目錄下,利用vim創(chuàng)建Podfile,在終端中輸入:
?vim Podfile
3、按 i 或 a ,進入插入模式,輸入以下內(nèi)容:
platform :ios, ‘7.0’
target ‘cocoapodsTest’ do ? ? ?//注意:target ‘工程名稱’ do
pod 'AFNetworking', '~> 3.1.0'
end
對上面4行文字的解釋:
platform :ios, ‘7.0’ ,表示當(dāng)前AFNetworking支持的iOS最低版本是iOS 7.0
target ‘cocoapodsTest’ do斥铺,當(dāng)前的目標(biāo)工程
pod 'AFNetworking', '~> 3.1.0'贬派,表示需要下載的AFNetworking版本是3.1.0
end 急但,表示結(jié)束
4、輸入完畢后,按【ESC】返回命令行模式搞乏,:(冒號)進入末行模式,輸入波桩,wq保存命令并退出vim
5、這時你會發(fā)現(xiàn)工程目錄中请敦,出現(xiàn)一個名為Podfile的文件镐躲,而且文件內(nèi)容就是剛剛輸入的內(nèi)容。
(注意,Podfile文件應(yīng)該和工程文件cocoapodsTest.xcodeproj在同一個目錄下)
6侍筛、利用CocoaPods下載AFNetworking類庫了,在終端中的當(dāng)前工程目錄下萤皂,運行以下命令:
?pod install
創(chuàng)建cocoapodsTest.xcworkspace成功時,終端提示如下:
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `cocoapodsTest.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total
pod installed.
同時發(fā)現(xiàn)在cocoapodsTest文件下勾笆,增加了cocoapodsTest.xcworkspace文件敌蚜,此時說明創(chuàng)建cocoapodsTest.xcworkspace已經(jīng)成功了。
7窝爪、特別注意
? ? target ‘cocoapodsTest’ do為必須輸入弛车,注意:target ‘工程名稱’ do,最新版本的cocoapods使用變化齐媒,否則無法生成 .xcworkspace文件,一直提示安裝報錯纷跛。
報錯:
[!] Invalid `Podfile` file: syntax error, unexpected keyword_end, expecting end-of-input.
#? from /Users/admin/Desktop/test15/Podfile:6
#? -------------------------------------------
#
>? end
#? -------------------------------------------
[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
8喻括、常見錯誤修復(fù)
報錯直接上圖,更加直觀:
產(chǎn)生此警告的原因是項目 Target 中的一些設(shè)置,CocoaPods 也做了默認(rèn)的設(shè)置,如果兩個設(shè)置結(jié)果不一致,就會造成問題贫奠。
解決方法?
打開項目 Target - Build Settings ,搜索 Other Linker Flags ,在這個設(shè)置上加入 $(inherited) 唬血。
打開項目 Target - Build Settings,依次搜索如上圖中所示的警告,根據(jù)提示的設(shè)置名稱,將這些設(shè)置選項全部改為 $(inherited) ,或者選中這些設(shè)置按下 delete 鍵恢復(fù)原設(shè)置唤崭。
如果有 FRAMEWORK_SEARCH_PATHS 這個設(shè)置的警告的話,最好先把當(dāng)前的設(shè)置項記錄下來,然后選中設(shè)置按下 delete 以后,再把之前的設(shè)置加進去,否則編譯可能會出現(xiàn)很多報錯拷恨。
然后重新執(zhí)行 pod install ?就會發(fā)現(xiàn)警告消失了。
如果我的方法不能夠解決你的問題的話,可以試一下網(wǎng)上的另一種方法,就是點擊項目文件 project.xcodeproj ,右鍵顯示包內(nèi)容,用文本編輯器打開 project.pbxproj ,command + F 搜索 OTHER_LDFLAGS ,刪除搜索到的設(shè)置,command + S 保存,然后重新執(zhí)行 pod install谢肾。