安裝cocoapods
sudo gem install cocoapods
用系統默認代理可能很慢,可以用淘寶的RubyGems鏡像來代替官方版本,執行以下命令:
$ gem sources -l
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l
安裝成功後,接着執行命令:
pod setup
如果Ruby環境不夠新,可能需要更新以下:
sudo gem update --system
使用CocoaPods的第一步,是在當前項目下,新建一個Podfile文件:
$ cd 項目路徑
$ touch Podfile
然後利用vim打開Podfile文件編輯,加入你想要使用的類庫,格式如下:
platform :ios
pod 'Reachability', '3.1.0'
platform:ios, '6.0'
pod 'SBJson','4.0.2'
pod 'SDWebImage+ExtensionSupport','3.7.1.2'
pod 'AFNetworking', '2.5.0'
確定定三方庫版本
pod search AFNetworking
如果是拷貝的別人的項目,或是一個很久沒打開過的項目,可能需要先執行一下:
pod update
最後一步,執行命令:
pod install
成功後會提示
Please close any current Xcode sessions and use XXXX.xcworkspace for this project from now on.
關於Podfile文件編輯時,第三方庫版本號的各種寫法:
pod ‘AFNetworking’ //不顯式指定依賴庫版本,表示每次都獲取最新版本
pod ‘AFNetworking’, ‘2.0’ //只使用2.0版本
pod ‘AFNetworking’, ‘>2.0′ //使用高於2.0的版本
pod ‘AFNetworking’, ‘>=2.0′ //使用大於或等於2.0的版本
pod ‘AFNetworking’, ‘<2.0′ //使用小於2.0的版本
pod ‘AFNetworking’, ‘<=2.0′ //使用小於或等於2.0的版本
pod ‘AFNetworking’, ‘~>0.1.2′ //使用大於等於0.1.2但小於0.2的版本,相當於>=0.1.2並且<0.2.0
pod ‘AFNetworking’, ‘~>0.1′ //使用大於等於0.1但小於1.0的版本
pod ‘AFNetworking’, ‘~>0′ //高於0的版本,寫這個限制和什麼都不寫是一個效果,都表示使用最新版本
CocoaPod安裝過程中常遇問題
問題一
[!] Unable to find a pod with name matching `AFNetworking'
解決方法
pod setup
問題二
[!] CocoaPods was not able to update the `master` repo. If this is an unexpected issue and persists you can inspect it running `pod repo update --verbose`
解決方法
1.可能是網絡問題,多試幾次
2.
pod repo remove master
pod setup
3.卸載重裝cocopad
先刪除全局的緩存:
$ sudo rm -fr ~/Library/Caches/CocoaPods/
$ sudo rm -fr ~/.cocoapods/repos/master/
$ sudo rm -fr Pods/
重裝
$ sudo gem install cocoapods
Podfile配置問題
[!] Unable to satisfy the following requirements:
- `AFNetworking (~> 2.6.3)` required by `Podfile`
Specs satisfying the `AFNetworking (~> 2.6.3)` dependency were found, but they required a higher minimum deployment target.