廢話不多,直接開始。
1.安裝rvm
1.1 一個管理Ruby環境的東西,在終端輸入 ?rvm -v?
如果顯示?-bash: rvm: command not found 說明沒有安裝rvm,使用這個命令安裝rvm:?curl -L get.rvm.io | bash -s stable?
使用?rvm list known 命令就可以查看Ruby版本信息
使用命令?rvm install 2.6.0 安裝需要的ruby版本 我安裝的時候出現這個錯誤
不知道怎么解決,有大神請賜教。
使用?rvm -v 檢查rvm版本
使用?ruby -v 檢查ruby 版本
1.2 后面終端輸入?gem update --system,更新gem,如果出現
說明權限不夠,繼續輸入?sudo gem update --system 接著輸入密碼
2、更換Ruby鏡像
首先移除現有的Ruby鏡像
終端輸入:$ gem sources --removehttps://rubygems.org/
然后添加國內最新鏡像源(淘寶的Ruby鏡像已經不更新了)
終端輸入:$ gem sources -ahttps://gems.ruby-china.org/
執行完畢之后輸入gem sources -l來查看當前鏡像
終端輸入:$ gem sources -l
如果結果是
*** CURRENT SOURCES ***
https://gems.ruby-china.org/
說明添加成功,否則繼續執行$ gem source -a https://gems.ruby-china.org/來添加
3、安裝CocoaPods
接下來開始安裝
終端輸入:$ sudo gem install cocoapods
如果出現這個
安裝錯誤提示
說明沒有權限,需要輸入
終端輸入:$ sudo gem install -n /usr/local/bin cocoapods
安裝成功如下:
安裝成功
到這之后再執行pod setup(PS:這個過程是漫長的,要有耐心)
終端輸入:$ pod setup
錯誤一:?error: RPC failed; curl 18 transfer closed with outstanding read data remaining
? fatal: The remote end hung up unexpectedly
? fatal: early EOF
? fatal: index-pack failed
[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
Cloning into 'master'...
remote: Counting objects: 2274525, done.? ? ? ?
remote: Compressing objects: 100% (344/344), done.? ? ? ?
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
解決方法:1、將curl的postBuffer值配置為500M,具體值看項目需求,配置成功不會有提示,配置失敗會有提示,終端輸入命令:
git config —globalhttp.postBuffer524288000
2、查看配置,輸入命令:
git config —list
終端會列出相關配置
credential.helper=osxkeychainfilter.lfs.clean=git-lfs clean -- %ffilter.lfs.smudge=git-lfs smudge -- %ffilter.lfs.process=git-lfs filter-processfilter.lfs.required=trueuser.name=ZBC-CWShttp.postbuffer=524288000core.repositoryformatversion=0core.filemode=truecore.bare=falsecore.logallrefupdates=truecore.ignorecase=truecore.precomposeunicode=trueremote.origin.url=https://github.com/ZBC-CWS/cocoapodsTest.gitremote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
查看其中的http.postBuffer對應的值是否是設置的值,如果是,設置成功,否則,設置失敗
3、重新執行pod相關命令,解決。
錯誤二:
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
? fatal: The remote end hung up unexpectedly
? fatal: early EOF
? fatal: index-pack failed
[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
Cloning into 'master'...
remote: Counting objects: 2275067, done.? ? ? ?
remote: Compressing objects: 100% (398/398), done.? ? ? ?
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
不知道怎么解決的,報錯以后繼續?pod setup 后面一次就成功了
4、CocoaPods的使用
1、首先我們來搜索一下三方庫
終端輸入:$ pod search AFNetworking
這時有可能出現
搜索不到的提示
這是因為之前pod search的時候生成了緩存文件search_index.json
執行rm ~/Library/Caches/CocoaPods/search_index.json來刪除該文件
然后再次輸入pod search AFNetworking進行搜索
這時會提示Creating search index for spec repo 'master'..
等待一會將會出現搜索結果如下:
AFNetworking搜索結果
出現這個了就說明搜索成功了,看一下上圖中的這一句:
pod 'AFNetworking', '~> 3.1.0'
這句話一會我們要用到,這是CocoaPods添加三方庫的關鍵字段
然后退出這個界面(這一步只是驗證一下cocoapods有沒有安裝成功,能不能搜索到你想要的三方庫),直接按"q"就退出去了。
如果搜索到的版本不是最新的,請查看iOS解決CocoaPods搜索不到最新版SDK的問題
2、在工程中創建一個Podfile文件
要想在你的工程中創建Podfile文件,必須先要進到該工程目錄下
終端輸入:$cd/Users/liyang/Desktop/CocoaPodsTest//這是我電腦上的路徑,你輸入你自己項目的路徑或直接拖拽也行
進來之后就創建
終端輸入:$ touch Podfile
然后你在你的工程目錄下可以看到多了一個Podfile文件
3、編輯你想導入的第三方庫的名稱及版本
使用vim編輯Podfile文件
終端輸入:$ vim Podfile
進入如下界面:
編輯Podfile文件
進來之后緊接著按鍵盤上的英文'i'鍵
下面的"Podsfile" 0L, 0C將變成-- INSERT --
然后就可以編輯文字了,輸入以下文字
platform:ios,'7.0'target'MyApp'dopod'AFNetworking','~> 3.1.0'end
解釋一下
platform :ios, '7.0'代表當前AFNetworking支持的iOS最低版本是iOS 7.0,
'MyApp'就是你自己的工程名字,
pod 'AFNetworking', '~> 3.1.0'代表要下載的AFNetworking版本是3.1.0及以上版本,還可以去掉后面的'~> 3.1.0',直接寫pod 'AFNetworking',這樣代表下載的AFNetworking是最新版。
編輯完之后成如下樣子:
編輯完成
此時該退出去了,怎么退出去呢?跟著我做,先按左上角的esc鍵,再按:鍵,再輸入wq,點擊回車,就保存并退出去了。
這時候,你會發現你的項目目錄中名字為Podfile的文件的內容就是你剛剛輸入的內容。
4、把該庫下載到Xcode中
終端輸入:$ pod install
這就開始下載了,需要一段時間,出現如下界面就說明安裝好了
安裝成功
這個時候關閉所有的Xcode窗口,再次打開工程目錄會看到多了一個后綴名為.xcworkspace文件。
工程目錄
以后打開工程就雙擊這個文件打開了,而不再是打開.xcodeproj文件。
進入工程后引入頭文件不再是#import "AFNetworking.h",而是#import
問題:pod install以后報錯
[!] Error installing YYWebImage
[!] /usr/bin/git clone?https://github.com/ibireme/YYWebImage.git/var/folders/v4/lv766dwx0bv7bdh3bh4yqfnr0000gn/T/d20180711-867-yxyegp --template= --single-branch --depth 1 --branch 1.0.5
Cloning into '/var/folders/v4/lv766dwx0bv7bdh3bh4yqfnr0000gn/T/d20180711-867-yxyegp'...
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
解決方法:多試幾次