最近在研究flutter,在flutter中引入第三方webview_flutter后,运行iOS设备时报错,具体报错如下:
Error running pod install
Error launching application on iPhone 15.

看问题时iOS的cocoapod在执行pod install命令下载第三方库时出现问题,如是我们找到项目中ios目录,使用xcode打开项目看看情况,
打开后发现项目包错Module 'webview_flutter_wkwebview' not found,很明显这里第三方没有下载下来

如是我们打开控制台cd 到项目的iOS目录,执行pod install这时候我们能看到控制台提示:- /Library/Ruby/Gems/2.6.0/gems/ffi-1.17.0-arm64-darwin/lib/2.6/ffi_c.bundle (LoadError)

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ffi_c (LoadError)
这个错误表明 Ruby 在尝试加载 ffi_c 时失败了,通常是因为 ffi gem 没有正确安装或与系统 Ruby 不兼容
重新安装 ffi gem
ffi gem 可能没有正确安装或损坏,可以尝试重新安装。
卸载ffi gem:
gem uninstall ffi
#如果报权限问题就使用
sudogem uninstall ffi
重新安装 ffi gem:
gem install ffi
#如果报权限问题就使用
sudo gem install ffi

升级ruby环境
本地环境很多依赖于默认的ruby环境,所以我们可以使用rvm管理和安装多个版本ruby
安装 rvm:
\curl -sSL https://get.rvm.io | bash -s stable
安装一个 Ruby 版本(如 3.0.0):
rvm install 3.0.0
rvm use 3.0.0 --default
验证 Ruby 版本:
ruby-v
查看所有ruby:
rvm list
在安装ruby的时候我出现了下面的包错
Error running '__rvm_make -j6',
please read /Users/apple/.rvm/log/1739849439_ruby-3.0.0/make.log

我怀疑指定的OpenSSL版本可能没生效,于是干脆通过brew uninstall openssl命令把最新版本的OpenSSL卸载了,再次执行上面的命令一切正常🎉!
解决方案
如果不局限于安装Ruby 3.0版本,那么可以通过安装更高的Ruby版本解决该问题,可以参考这篇文章RVM - 安装最新Ruby版本。
如果一定要安装Ruby 3.0版本,请安装1.1版本的OpenSSL,并卸载最新版本,同时指定使用HomeBrew安装的OpenSSL完成安装:
安装1.1版本的OpenSSL
brew install openssl@1.1
卸载最新版本的OpenSSL
brew uninstall openssl
指定使用HomeBrew安装的OpenSSL完成安装
rvm install ruby-3.0.0 --with-openssl-dir=`brew --prefix openssl`
如果不想卸载最新版本,可以通过brew link命令切换(链接)openssl的版本完成安装:
安装1.1版本的OpenSSL
brew install openssl@1.1
切换OpenSSL的版本为1.1
brew link --overwrite openssl@1.1
--overwrite参数的作用是强制切换。如果不使用该参数,可以先执行brew unlink openssl命令后再执行brew link openssl@1.1命令完成切换。
指定使用HomeBrew安装的OpenSSL完成安装
rvm install ruby-3.0.0 --with-openssl-dir=`brew --prefix openssl@1.1`

执行完成后再次执行pod install 报错:`find_spec_for_exe':can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)

因使用 brew 安装工具导致 ruby 环境错乱, 执行pod install时报错提示找不到 gem 可执行文件
Traceback (most recent call last): 2: from /usr/local/bin/pod:23:in `' 1: from /Library/Ruby/Site/2.6.0/rubygems.rb:294:in `activate_bin_path'/Library/Ruby/Site/2.6.0/rubygems.rb:275:in `find_spec_for_exe': can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)
解决办法:
重新安装 ruby 环境(默认安装最新版本)
rvm reinstall ruby --disable-binary
运行结果
mruby-1.3.0 - #removing src/mruby-1.3.0 - please waitmruby-1.3.0 - #removing rubies/mruby-1.3.0 - please waitRVM does not have prediction for required space for mruby-1.3.0, assuming 150MB should be enough, let us know if it was not.Checking requirements for osx.Certificates bundle '/usr/local/etc/openssl@1.1/cert.pem' is already up to date.Requirements installation successful.Installing Ruby from source to: /Users/jack/.rvm/rubies/mruby-1.3.0, this may take a while depending on your cpu(s)...mruby-1.3.0 - #downloading 1.3.0, this may take a while depending on your connection...mruby-1.3.0 - #extracting 1.3.0 to /Users/jack/.rvm/src/mruby-1.3.0 - please waitmruby-1.3.0 - #compiling - please waitmruby-1.3.0 - #installing - please waitInstall of mruby-1.3.0 - #completeRequired ruby-2.7.0 is not installed.To install do: 'rvm install "ruby-2.7.0"'
重新安装 cocoapods
gem install cocoapods
运行结果:
Successfully installed cocoapods-1.9.3Parsing documentation for cocoapods-1.9.3Done installing documentation for cocoapods after 1 seconds1 gem installed
再重新执行pod install就OK了
Homebrew,是Mac OS X上的软件包管理工具,使用起来非常方便,安装任意软件包时 brew 会自动下载其依赖;
RubyGems 提供了ruby社区gem的托管服务,主要用于下载、安装使用 ruby 软件包
平常 iOS 开发使用 cocoapods 等工具都是使用 gems 进行安装管理,当使用 brew 安装软件包时有可能因依赖导致 ruby 环境错乱,不建议混合使用(使用 brew 也可以安装 cocoapods 而且很方便)
再次运行项目,又发现新的包错:'Flutter/Flutter.h not found'
这里这样做
1、为了保守起见先备份ios文件
2、删除iOS文件

3、cd到项目跟目录执行命令重新创建ios项目
flutter create .
4、将备份的ios重要文件替换进来
详细见:https://www.kindacode.com/article/flutter-fatal-error-flutter-flutter-h-file-not-found