安裝
- 安裝Ruby
CocoaPods基於Ruby語言開發而成,因此安裝CocoaPods前需要安裝Ruby環境。幸運的是Mac系統默認自帶Ruby環境,如果沒有請自行查找安裝。檢測是否安裝Ruby:$ gem -v2.0.14
安裝則會提示當前Ruby版本。gem介紹:gem是一個管理Ruby庫和程序的標准包,它通過Ruby Gem(如 http://rubygems.org/ )源來查找、安裝、升級和卸載軟件包,非常的便捷。
- 更換gem源
因為國內網絡的問題導致gem源間歇性中斷,原因你懂的。因此我們需要更換gem源,使用淘寶的gem源[https://ruby.taobao.org/.](https://ruby.taobao.org/%E3%80%82)
第一步:移除默認的源gem sources --remove https://rubygems.org/
第二步:指定淘寶的源gem sources -a https://ruby.taobao.org/
第三步:查看指定的源是不是淘寶源$ gem sources -l *** CURRENT SOURCES *** https://ruby.taobao.org/
如果是https://ruby.taobao.org/,則更換成功。
- 安裝CocoaPods
改成淘寶源后執行以下命令進行安裝:sudo gem install cocoapods
如果換成國內鏡像使用 gem install cocoapods 報錯:ERROR: While executing gem ... (Gem::DependencyError) Unable to resolve dependencies: cocoapods requires cocoapods-core (= 1.1.1), cocoapods-downloader (< 2.0, >= 1.1.2), cocoapods-trunk (< 2.0, >= 1.1.1), xcodeproj (< 2.0, >= 1.3.3)
執行:sudo gem update --system && sudo gem install cocoapods
報錯:ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/xcodeproj
執行:sudo gem install -n /usr/local/bin cocoapods
安裝完成,輸入以下命令檢測是否安裝成功:$ pod --version 1.1.1
cd到自己的項目目錄下
創建Podfilevim Podfile
如果沒有創建Podfile
文件將無法安裝, CocoaPods都不知道改安裝說明, 怎么去安裝呢, 對吧.[!] No `Podfile' found in the project directory.
輸入以下代碼(配置文件,需要添加的第三方):
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
inhibit_all_warnings!
target '工程名' do
pod 'AFNetworking', '~> 2.6.0'
end
然后保存退出
esc:wq
執行pod update
OR pod install
。
如果報錯:
[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master`. You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`.
執行:sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
(xcode目錄)
紅色部分是我的Xcode的路徑,注意上面的代碼中的"--"和"select"之間有一個空格。
解決方法可以使用一下代碼:
又產生的問題:一般這個時候會輸出--unalbe to get active developer directory
defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE;killall Finder
這樣的話就可以了,然后用你的Xcode的路徑代替上面的紅色的部分就可以了。
問題描述
1:[!] The 'master' repo requires CocoaPods 1.0.0 - (currently using 0.39.0) Update CocoaPods, or checkout the appropriate tag in the repo.
問題原因:沒有更新到最新cocoapods版本
sudo gem install -n /usr/local/bin cocoapods –pre
如果報錯或其他問題
sudo gem update --system && sudo gem install cocoapods
出現報錯信息: ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/xcodeproj
執行: sudo gem install -n /usr/local/bin cocoapods
輸入以下命令檢測是否成功: pod --version 1.1.1
2:[!] The dependency `AFNetworking (~> 2.6.0)` is not used in any concrete target.
等
問題原因:podfile升級到最新版本,pod里的內容必須明確指出所用第三方庫的target
可以修改Podfile文件的配置文件,讓它兼容不指定固定版本;且又不報錯
platform :ios, '8.0'
inhibit_all_warnings!
target '工程名' do
pod 'AFNetworking', '~> 2.6.0'
end
3:Updating local specs repositories
遇到pod install或者pod update長時間卡在Updating local specs repositories
- 常見的解決方式是跳過更新cocoapods的spec倉庫
pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
- 最好的辦法是
pod repo remove master
pod setup
pod install
4:[!]Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
不要使用文本編輯去編輯Podfile,使用Xcode編輯,或者使用終端敲命令去編輯。或者輸入格式錯誤,沒輸入運行版本:$platform:ios, ‘8.0‘
5:使用cocoapods導入第三方類庫后頭文件沒有代碼提示?
選擇Target -> Build Settings 菜單,找到\”User Header Search Paths\”設置項,新增一個值"${SRCROOT}",並且選擇\”Recursive\”
6:[!]Unable to find a target named ' ';
Podfile的工程文件和建立工程名不一致。名字改為一致即可
7:syntax error ,unexpected end-of-input,expecting keyword_end
podfile寫法有問題,使用標准的podfile的寫法
platform :ios, '8.0'
inhibit_all_warnings!
target '工程名' do
pod 'AFNetworking', '~> 2.6.0'
end
8: Analyzing dependencies
[!]Could not automatically select an Xcode project . Specify one in your Podfile like 50 : xcodeproj ' path / to / Project . xcodeproj
自己建立了一個空文件夾,沒有建立工程
處理方法:刪除這個空工程,重新建立一個工程,再按照上面知識點講解一步步來即可。
9:pod 導入后出現"_OBJC_CLASS_$_xxx"
target->build phases->link binary with libraries添加xxx.a
如果不是pod導入的類庫,出現這種問題
原因:在創建類的時候沒有勾選"add to targets"
target->build phases->compile sources 添加.h,.m文件
或(建議用上面)
在Build Setting 中的Other Linker Flags選項中加入$(OTHER_LDFLAGS)
10:Xcode - ld: library not found for -lPods
當新增加一個Target,並且pod install后,由於創建項目是默認創建的Target的Build Phases中引用了舊的.a,例如libPods.a,當新增加Targget后,libPods.a已經變成了libPods-Test.a,而新增加的Target名字為Second,依賴的.a為libPods-Second.a。所以libPods.a此時已經不再被引用,並且不會被生成,如果任何地方引用了就會報錯,解決辦法是出現問題的Target的Build Phases中刪除無用的.a引用,例如libPods.a
target->build phases->link binary with libraries
11:pod 導入庫,找不到頭文件
在TARGETS -> Search Paths -> User Header Search Paths 中 寫入 ${SRCROOT}再將后面參數改為recursive:
12:如果要移除Cocoapods,則可以:
a. 刪除工程文件夾下的Podfile、Podfile.lock及Pods文件夾
b. 刪除xcworkspace文件
c. 使用xcodeproj文件打開工程,刪除Frameworks組下的Pods.xcconfig及libPods.a引用
d. 在工程設置中的Build Phases下刪除Check Pods Manifest.lock及Copy Pods Resources
不小心刪除pods.xcconfig,出現diff: /../Podfile.lock: No such file or directory
sudo pod install一下就好了
如果編譯的時候出現權限問題,對工程文件夾$sudo chmod 777 path-to-project-folder/*
$sudo chown 777 path-to-project-folder/*
13:出現/Library/Ruby/Gems/2.0.0/gems/claide-0.8.1/lib/claide/command.rb:417:in `help!': [!] You cannot run CocoaPods as root. (CLAide::Help)
重新打開一個終端,然后pod install,不要sudo pod install。
14:刪除CocoaPods版本,並安裝指定版本
比如我本來安裝完CocoaPods 1.0.0版,但發現它實現跟先前還是差別滿大的,決定降回0.39這個比較穩定版本;就可以用命令先刪除本地的CocoaPods版本,再指定安裝特定版本;
a 卸載 cocoapods(它會提示你要刪除本地哪個版也可以全刪)
sudo gem uninstall cocoapods
b 安裝 0.39版本
sudo gem install -n /usr/local/bin cocoapods -v 0.39
15:更新 CocoaPods 或者以前手動添加過第三方SDK改用Cocopods,可能會出現以下的錯誤
錯誤1:
[!] The `Paopao [Debug]` target overrides the `PODS_ROOT` build setting defined in `Pods/Target Support Files/Pods/Pods.debug.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or - Remove the build settings from the target. [!] The `Paopao [Debug]` target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support Files/Pods/Pods.debug.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target. [!] The `Paopao [Release]` target overrides the `PODS_ROOT` build setting defined in `Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target. [!] The `Paopao [Release]` target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target.
產生此警告的原因是項目 Target 中的一些設置,CocoaPods 也做了默認的設置,如果兩個設置結果不一致,就會造成問題。
我想要使用 CocoaPods 中的設置,分別在我的項目中定義PODS_ROOT
和 Other Linker Flags
的地方,把他們的值用$(inherited)
替換掉,進入終端,執行
pod update
警告沒了
一種簡單粗暴的方法:
點擊項目文件 project.xcodeproj,右鍵顯示包內容
,用文本編輯器打開project.pbxproj
,刪除OTHER_LDFLAGS
的地方,保存,pod update
,警告沒了。。。。
16:
17: