问题场景
使用flutter doctor -v
出现以后警告
[!] Xcode - develop for iOS and macOS (Xcode 12.4) ✗ CocoaPods installed but not working. You appear to have CocoaPods installed but it is not working. This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it. This can usually be fixed by re-installing CocoaPods. For more info, see https://github.com/flutter/flutter/issues/14293. To re-install CocoaPods, run: sudo gem install cocoapods
然后将运行项目在IOS中,出现以下报错
Launching lib/main.dart on iPhone 12 in debug mode... Warning: CocoaPods is installed but broken. Skipping pod install. You appear to have CocoaPods installed but it is not working. This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it. This can usually be fixed by re-installing CocoaPods. For more info, see https://github.com/flutter/flutter/issues/14293. To re-install: sudo gem install cocoapods CocoaPods not installed or not in valid state. Error launching application on iPhone 12.
解决方法
看错误提示的是Ruby的版本和CocoaPods的版本不对应产生的问题。
安装RVM
Ruby Version Manager(Ruby版本管理器)是用于类UNIX操作系统的软件平台,用于管理同一设备上Ruby的多个安装。
$ curl -L https://get.rvm.io | bash -s stable
安装完成后,根据提示使用source
命令使rvm生效。
# 根据自己电脑使用的环境变量使用对应的命令,如果不知道就2条命令都使用。 $ source ~/.bash_profile $ source ~/.bashrc
接着关闭Terminal,然后再重新打开。
安装ruby 2.6版本
使用上面安装的RVM
来安装ruby 2.6
$ rvm install ruby-2.6
安装完成后,会显示安装的具体版本。笔者写这篇文章的时候2.6最新的版本是2.6.6。
接着切换ruby版本
$ rvm use ruby-2.6.6
设置当前版本为默认版本,这样一来以后新打开的控制台默认的 Ruby 就是这个版本
$ rvm --default use 2.6.6
最后重新安装CocoaPods
$ sudo gem install cocoapods
这样安装的pod会自动跟当前的Ruby版本对应上。
再次运行flutter doctor
就不会有错误了。