CocoaPods能夠方便地經過Mac自帶的RubyGems安裝。ios
打開Mac電腦自帶的終端,而後按照如下操做便可:swift
首先查看ruby當前源 vim
$ gem sources -l *** CURRENT SOURCES *** https://rubygems.org/
移除ruby當前源ruby
$ gem sources --remove https://rubygems.org/
替換國內最新鏡像源(淘寶的Ruby鏡像已經不更新了,https://gems.ruby-china.org域名更新爲https://gems.ruby-china.com)ide
$ gem sources -a https://gems.ruby-china.com/
再次查看ui
$ gem sources -l *** CURRENT SOURCES *** https://gems.ruby-china.org
若是使用 $ gem update --system spa
會報錯誤 debug
ERROR: While executing gem ... (Gem::FilePermissionError)code
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directoryorm
這時使用
$ sudo gem update --system
升級成功後會提示
RubyGems system software updated
若是使用 $ sudo gem install cocoapods
報錯誤
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory
那就使用
$ sudo gem install -n /usr/local/bin cocoapods
而後執行pod setup (ps:這個執行很慢,耐心等待...... pod setup在執行時,會輸出Setting up CocoaPods master repo,而後會卡一下子。這步實際上是 Cocoapods 將它的信息下載到 ~/.cocoapods目錄下,你能夠command+n
新建一個終端窗口,執行cd ~/.cocoapods/
進入到該文件夾下,而後du -sh *
查看文件大小,這個目錄最終大小是900多M。出現Setup
completed 的時候說明已經完成了
$ pod setup
①首先搜索須要的三方庫 AFNetworking,會出現符合條件的搜索結果
$ pod search AFNetworking
②在工程中建立Podfile文件,輸入終端命令進入到工程目錄下,而後建立Podfile文件
$ cd /Users/Desktop/YourAPPName $ vim Podfile
③進入界面後,輸入
platform :ios, '8.0' target 'YourApp' do pod 'AFNetworking', '~> 3.1.0' end
④把三方庫下載到工程中
$ pod install
注:若是是在swift的工程中引入三方庫,pod install 可能出現下面錯誤,處理辦法
[!] The `GWeather [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-GWeather/Pods-GWeather.debug.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target.
找到【Project/Target】-->【Your Project Name】-->【Buid Settings】
搜索‘ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES’,選擇「other」,輸入 $(inherited)