關(guān)于CocoaPods,是OS X和iOS下的一個第三方類庫管理工具,它的好處這里不多說,主要說下如何安裝CocoaPods。
CocoaPods的安裝需要Ruby環(huán)境,Mac系統(tǒng)都自帶Ruby,通過下面終端命令查看Ruby版本:
ruby -v
CocoaPods支持的Ruby最低版本是2.2.2,如果自己的版本低于2.2.2就需要更新升級Ruby:
sudo gem update --system
我的沒更新
更換Ruby鏡像
Ruby默認(rèn)的源地址是國外網(wǎng)絡(luò)地址,通過下面終端命令查看當(dāng)前鏡像地址:
gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
首先移除當(dāng)前鏡像
gem sources --remove https://rubygems.org/
然后添加國內(nèi)最新Ruby鏡像地址
gem sources -a https://gems.ruby-china.com/
https://gems.ruby-china.com/added to sources
這時候再查看鏡像地址就是這樣的了
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
安裝CocoaPods
Ruby環(huán)境安裝好以后,接下來就是安裝CocoaPods,終端輸入:
sudo gem install cocoapods
報錯
Building native extensions. This could take a while...
ERROR:? Error installing cocoapods:
ERROR: Failed to build gem native extension.
? ? current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.14.2/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /Library/Ruby/Site/2.6.0 -r ./siteconf20210113-23968-1w6mjuz.rb extconf.rb
checking for ffi.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.? Check the mkmf.log file for more details.? You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME)
--with-ffi_c-dir
--without-ffi_c-dir
--with-ffi_c-include
--without-ffi_c-include=${ffi_c-dir}/include
--with-ffi_c-lib
--without-ffi_c-lib=${ffi_c-dir}/lib
--enable-system-libffi
--disable-system-libffi
--with-libffi-config
--without-libffi-config
--with-pkg-config
--without-pkg-config
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in `block in try_compile'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:534:in `with_werror'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1109:in `block in have_header'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:331:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:331:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1108:in `have_header'
from extconf.rb:10:in `system_libffi_usable?'
from extconf.rb:42:in `'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
? /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.14.2/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.14.2 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.14.2/gem_make.out
原因是因為
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/universal-darwin19/rbconfig.rb:找不到這個
檢查一下本地的目錄是
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/universal-darwin20/rbconfig.rb:
注意
在找ruby-2.6.0的時候,因為每個人的ruby環(huán)境不一樣所以要注意自己的目錄是否是對的上我的是ruby 2.6.3,所以不建議直接復(fù)制目錄cd進(jìn)去,一步步找自己本地的文件
我的路徑是
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0
解決方案
報錯里提示universal-darwin19 本地的卻是universal-darwin20
所以將20復(fù)制粘貼將universal-darwin20文件改成universal-darwin19

再執(zhí)行sudo gem install cocoapods
報錯
ERROR:? While executing gem ... (Gem::FilePermissionError)
? ? You don't have write permissions for the /usr/bin directory.
執(zhí)行sudo gem install cocoapods -n /usr/local/bin
出現(xiàn)
Done installing documentation for fuzzy_match, httpclient, algoliasearch, thread_safe, tzinfo, activesupport, cocoapods-core, cocoapods after 17 seconds
8 gems installed
說明執(zhí)行成功了
然后
~ % pod setup
一瞬間就出現(xiàn)Setup completed,不要懵
這個時候 不要直接去 使用Search 搜索第三方庫? 因為會找不到? ?直接先去找個項目 pod init,然后給Podfile 進(jìn)隨便添加一個庫? 如:???pod 'SwiftyJSON' 。 然后執(zhí)行 pod install ?

出現(xiàn)以上圖片信息 則說明 第三方庫引入成功? ?然后再去執(zhí)行Search命令? 就可以查找到第三方庫了
例如% pod search afn
按Q結(jié)束搜索,復(fù)制pod 'AFNetworking', '~> 4.0.1'
至此,Mac下安裝CocoaPods成功
參考文章:
http://www.itdecent.cn/p/39f22e82c97f
http://www.itdecent.cn/p/7ea686bc2a1a
https://stackoverflow.com/questions/50422786/you-dont-have-write-permissions-for-the-usr-bin-directory-while-installing-co
http://www.itdecent.cn/p/1e093c2ae4e2