[记录]iOS项目中集成flutter的一些注意点

这是官方链接

记录几点描述不清楚的地方。

1. flutter module文件夹需要与iOS项目的根目录同级

例如:iOS项目文件目录为 /Desktop/iOSProject,那么执行flutter create -t module my_flutter 的时候 需要在/Desktop目录下执行

2.Podfile中添加flutter path,如下

// 这里没必要写绝对路径,否则多人协作开发需要一直改
flutter_application_path = '../flutter_module/'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

3.关于[!] Invalid 'Podfile' file: [!] Specifying multiple 'post_install' hooks is unsupported.的报错

我们使用cocoapod时候可能会遇到这个警告[The iOS Simulator deployment targets is set to 7.0, but the range of supported deployment target version for this platform is 8.0 to 12.1](https://stackoverflow.com/questions/54704207/the-ios-simulator-deployment-targets-is-set-to-7-0-but-the-range-of-supported-d)

那么我们一般会选择在 Podfile 中添加如下代码片段来解决这些警告

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'].to_f < 8.0
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
      end
    end
  end
end

但是由于 flutter_module/.ios/Flutter/podhelper.rb (在flutter module文件夹中 shift+command+. 可以查看隐藏文件夹) 中也存在 post_install do |installer| 这个命令,而cocoapods又不支持多个这命令,所以我们需要把Podfile中这部分代码删除,可以在podhelper.rb中做如下更改

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
            next if  config.base_configuration_reference == nil
            xcconfig_path = config.base_configuration_reference.real_path
            File.open(xcconfig_path, 'a+') do |file|
                file.puts "#include \"#{File.realpath(File.join(framework_dir, 'Generated.xcconfig'))}\""
            end
            #下面这部分是新增的
            if config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'].to_f < 8.0
                config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
            end
        end
    end
end

4.集成flutterboost遇到的问题

集成之后,需要package get,然后pod install

可能会遇到这个错误'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based

podtarget中选中flutter_boost,然后做如下修改

参考链接

推荐阅读更多精彩内容