iOS Flutter混编环境配置(最新)

13 篇文章 1 订阅


flutter是什么就不多做介绍了,目前使用纯flutter进行开发的大型项目还比较少,多数试水的还是打算和现有的原生代码进行混编,即将flutter页面嵌入到原生app中。而网上大部分都是讲安卓和flutter混编的经验,有iOS和flutter混编的也是比较老的方法,现在已经不再适用。这时这几天混编配置的方法记录。

一、创建flutter module

都需要保证创建的flutter工程和原生应用在同一目录下。
假如你在some/path/MyApp存在一个iOS原生应用,要创建flutter工程:

$ cd some/path/
$ flutter create -t module my_flutter

这会创建在这个位置**some/path/my_flutter/创建一个flutter module,里面包含基础的dart代码,以及一个.ios/**的隐藏文件,其中包含Cocoapods和helper Ruby script。

二、添加原生app对flutter的依赖

如果你的项目中已经在使用CocoaPods,可以直接按照以下的步骤进行配置,如果没有使用CocoaPods,则需要先初始化一个Podfile文件(在工程目录,执行pod init)。
1.添加以下内容到Podfile中:
注意:需要添加到文件顶部的位置

flutter_application_path = 'path/to/my_flutter/'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

在这里插入图片描述
2.每一个Xcode target都需要绑定flutter,调用install_all_flutter_pods(flutter_application_path)

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

flutter_application_path = 'my_flutter/'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'hello' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
install_all_flutter_pods(flutter_application_path)
  # Pods for hello

  target 'helloTests' do
    inherit! :search_paths
    # Pods for testing
install_all_flutter_pods(flutter_application_path)
  end

  target 'helloUITests' do
    # Pods for testing
install_all_flutter_pods(flutter_application_path)
  end

end

3.命令行执行 pod install
任何时候,如果你修改了flutter工程中some/path/my_flutter/pubspec.yaml中的三方依赖,在flutter工程所在目录,都需要执行flutter packages get来更新从podhelper.rb脚本中读取的plugins列表,然后在app所在目录执行 pod install 。
4.由于flutter工程不支持bitcode,所以需要在xcode中设置Build Settings->Build Options->Enable Bitcode为NO。

使用

打开
在这里插入图片描述
在这里插入图片描述

#import <Flutter/Flutter.h>
- (void)jumpToFlutterVC {
    FlutterViewController *flutterVC = [[FlutterViewController alloc] init];
   // flutterVC.title = @"flutterVC";//这里没起作用?
    [flutterVC setInitialRoute:@"route2"];//这里没起作用?
    flutterVC.view.backgroundColor = [UIColor redColor];
    
    [self presentViewController:flutterVC animated:YES completion:nil];
}

在这里插入图片描述

注意

以前,需要将构建阶段添加到现有Xcode项目中,现在已经不需要了

"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed

重新生成podhelper.rb

如何点击运行报错:
Dart Error: Can’t load Kernel binary: Invalid kernel binary format version.
在这里插入图片描述
执行:

cd 'path/to/my_flutter/'
rm .ios/Flutter/podhelper.rb
flutter build ios

实践用VScode编写flutter_module

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
修改后Xcode 中
在这里插入图片描述
在这里插入图片描述

实用问题汇总

##1: flutter doctor 没反应,VSCode也一直select a device to use
先终端执行一下

git

然后就可以正常使用了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值