CocoaPods 學習筆記 什麼是CocoaPods? CocoaPods 是用 Ruby 語言所開發出來的管理工具,透過 CocoaPods 可以輕鬆管理 Swift 和 Objective-C 所使用的第三方資源庫。從下載、匯入、建構環境,到最後的更新一手包辦。
CocoaPods安裝 其實MAC是自帶Ruby的,不過安裝前還是先更新一下Ruby比較好。
1. 查看當前Ruby版本
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
2. 升級Ruby,首先要先安裝rvm
curl - L get .rvm.io | bash - s stable
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
3. 安裝完rvm還要設定環境參數
source ~/ .bashrc
source ~/ .bash_profile
4. 查看rvm版本
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
5. 出可安裝的Ruby版本,然後看最近的版本號是多少,這時最新版本為 3.0
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
6. 安裝Ruby最新版本,這裡是安裝3.0喔
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
7. 安裝完成後,再次確認 Ruby 版本是否正確
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
8. 開始安裝 CocoaPods
sudo gem install cocoapods
9. 安裝完後,開始要讓 CocoaPods 自己設定了
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
10. 確認 CocoaPods 版本
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
– – – (鐵人賽Day02)
開始使用CocoaPods 1. CocoaPods初始化 先建立一個Xcode專案(我的名稱叫"CocoaPods_practise")
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
2. 開啟終端機並cd到剛剛建立專案的位置
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
3. 輸入初始化的指令,你會發現專案的資料夾裡多出了一個叫"Podfile"的文字檔,這個文字檔是要給CocoaPods看的,簡單來說就是 CocoaPods 的 Profile
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
4. 接著我們要開始編輯 Podfile,編輯的方法有很多種,可以用 mac 內建的 vim,或是連點兩下用文字編輯器,還是要安裝其他編輯器都可以。 這邊我用vim來做編輯,輸入以下指令:
Podfile 初始的內容就像這樣
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
這邊我用很熱門的Firebase這個第三方庫來做範例。
不過這時候應該很多人會納悶,我們怎麼知道有哪些第三方庫呢?可以去 CocoaPods ( https://cocoapods.org/ ) 的網頁去查詢有哪些第三方庫,或是可以上Google查都有很多喔!
5. 回到編輯Podfile,我們在 # Pods for CocoaPods_practise 後加入一行『pod 'Firebase'』,這代表我們要使用Firebase這個第三方庫。 使用vim小提示:
如果要輸入需要先按下『 i 』,左下角變成『 – INSERT – 』才有辦法輸入喔!
輸入完要存檔,要先按下『 esc 』,左下角的『 – INSERT – 』不見,然後輸入『 :wq 』(會顯示在左下角),按下 Enter 即可。
Podfile 內容就會變成這樣:
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
6. Podfile 都編輯完後,在 Terminal 輸入以下指令: 7.等待貼心的 CocoaPods 安裝完後,他會顯示『 [!] Please close any current Xcode sessions and use 'CocoaPods_practise.xcworkspace' for this project from now on. 』,意思就是要我們把原先開啟的>』,意思就是要我們把原先開啟的 xcodeproj 關閉,然後開啟 CocoaPods 建好的『xcworkspcae 檔』
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
8. 再次打開專案資料夾,會發現多了很多東西。找到 CocoaPods 所說的『xcworkspcae 檔』
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
9. 開啟 Xcode 之後會發現,除了有我們之前的專案之外多了一個Pods專案,CocoaPods就是利用這個專案來幫我們管理以及連結第三方庫
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
10. 接下來我們就可以使用這些第三方庫,但是別忘了還是要自己 import這些函式庫喔!
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
CocoaPods第三方庫更新 CocoaPod最方便的就是可以用一行指令更新所有第三方庫
開啟終端機並輸入以下指令:
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
CocoaPods 刪除部分第三方庫 1. 開啟專案裡的Podfile並把要刪除的第三方庫刪除就好。 我們拿Firebase為例
原本的Podfile:
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
刪除後:
Image Not Showing
Possible Reasons
The image file may be corrupted The server hosting the image is unavailable The image path is incorrect The image format is not supported
Learn More →
2. 在終端機輸入以下指令(終端機的位置也要cd到專案資料夾):
3. 刪除完第三方庫後,記得要把程式碼有使用到這些第三方庫的 API 刪掉喔!
把CocoaPods從專案全部移除 1. 開啟專案資料夾,把Podfile、Profile.lock、CocoaPods_practise.xcworkspace(這跟據專案名稱不同會有所不同) 這三個檔案刪掉,再把Pods這個資料夾刪掉。
2. 開啟.xcodeproj檔
3. 刪除Pods資料夾和Frameworks資料夾內報紅的framework(請勿將Frameworks刪除)
4. 開啟Build Phases
5. 刪除 [CP] Check Pods Manifest.lock
6. 這樣就成功的把CocoaPods全部從專案移除掉了