关于CocoaPods,是OS X和iOS下的一个第三方类库管理工具,它的好处这里不多说,主要说下如何安装CocoaPods。
CocoaPods的安装需要Ruby环境,Mac系统都自带Ruby,通过下面终端命令查看Ruby版本:
ruby -v
CocoaPods支持的Ruby最低版本是2.2.2,如果自己的版本低于2.2.2就需要更新升级Ruby:
sudo gem update --system
我的没更新
更换Ruby镜像
Ruby默认的源地址是国外网络地址,通过下面终端命令查看当前镜像地址:
gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
首先移除当前镜像
gem sources --remove https://rubygems.org/
然后添加国内最新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环境安装好以后,接下来就是安装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环境不一样所以要注意自己的目录是否是对的上我的是ruby 2.6.3,所以不建议直接复制目录cd进去,一步步找自己本地的文件
我的路径是
/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复制粘贴将universal-darwin20文件改成universal-darwin19
再执行sudo gem install cocoapods
报错
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /usr/bin directory.
执行sudo gem install cocoapods -n /usr/local/bin
出现
Done installing documentation for fuzzy_match, httpclient, algoliasearch, thread_safe, tzinfo, activesupport, cocoapods-core, cocoapods after 17 seconds
8 gems installed
说明执行成功了
然后
~ % pod setup
一瞬间就出现Setup completed,不要懵
这个时候 不要直接去 使用Search 搜索第三方库 因为会找不到 直接先去找个项目 pod init,然后给Podfile 进随便添加一个库 如: pod 'SwiftyJSON' 。 然后执行 pod install
出现以上图片信息 则说明 第三方库引入成功 然后再去执行Search命令 就可以查找到第三方库了
例如% pod search afn
按Q结束搜索,复制pod 'AFNetworking', '~> 4.0.1'
至此,Mac下安装CocoaPods成功
参考文章:
https://www.jianshu.com/p/39f22e82c97f
https://www.jianshu.com/p/7ea686bc2a1a
https://stackoverflow.com/questions/50422786/you-dont-have-write-permissions-for-the-usr-bin-directory-while-installing-co
https://www.jianshu.com/p/1e093c2ae4e2