cocoapods是一個用來管理第三方庫的軟件,使用cocospods可以幫助我們自動化、集中、快速的管理第三方庫。
一、配置Ruby環境
cocoapods使用Ruby實現,運行cocoapods需要Ruby環境。
//OS X 10.9以后自帶Ruby環境,使用
ruby -v
查看當前Ruby版本
//如果直接安裝,因為鏈接不上,所以是安裝不了的,首先我們需要先把原來的RubyGems替換成馬云的。
//移除
gem sources --remove https://rubygems.org/
//替換
gem sources -a https://ruby.taobao.org/
//查看目前鏡像文件
gem sources -l
設置成功,就可安裝cocoapods了
二、安裝
gem install cocoapods
安裝時出現了如下錯誤
ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
sudo gem install -n /usr/local/bin cocoapods
之后輸入密碼,重新安裝
sudo gem install cocoapods
現在如下提示,代表安裝完成
1 Installing ri documentation for cocoapods-core-0.39.02 Parsing documentation for cocoapods-0.39.03 Installing ri documentation for cocoapods-0.39.04 3 gems installed
//設置
pod setup
設置可能需要一些時間,耐心等待,直到出現
1 create mode 100644 "Specs/\360\237\224\222/0.2.0/\360\237\224\222.podspec.json"2 From https://github.com/CocoaPods/Specs3 fea4730..86a39b3 master -> origin/master4 Checking out files: 100% (16708/16708), done.5 Setup completed
設置完成
查看版本
pod --version
三、使用
進入項目所在目錄 cd .xcodeproj同一層
// 建立Podfile文件,文件名必須叫這個,Podfile文件決定你項目需要引入的庫
touch Podfile
// 編輯
vi Podfile
輸入i開始輸入,示例如下
platform:ios,'7.0' //庫支持的最底版本為7.0
pod 'AFNetworking','~>2.0'
pod 'AFNetworkActivityLogger'
編輯完成后,按ESC鍵+shift+冒號,再在Terminal里面輸入wq退出
我開始的時候寫成下面這樣,沒有在后面標注版本號
platform:ios
導致安裝AFNetworkActivityLogger時報以下錯誤
[!] Unable to satisfy the following requirements:-AFNetworkActivityLogger (~> 2.0.4)
required byPodfile
Specs satisfying theAFNetworkActivityLogger (~> 2.0.4)
dependency were found, but they required a higher minimum deployment target.
//引入第三方庫
pod install
pod install --verbose --no-repo-update
更新第三方庫的引用
// 打開Podfile配置文件,添加或刪除配置文件中的第三方庫引用,保存退出之后,運行:
pod update
// 搜索第三方庫
pod search 類庫名,支持模糊查詢(如:AFNetworking)
出現如下提示說明引入成功
[!] Please close any current Xcode sessions and useCocoaPodsText.xcworkspace
for this project from now on.Sending stats
退出Xcode,從新的CocoaPodsText.xcworkspace文件進入項目
項目里你看到的應該是下面這樣
在你需要用到這些庫的時候,用如下方式引入
#import "ViewController.h"
#import <AFNetworking.h>
#import <AFNetworkActivityLogger.h>
? 原文地址