Flutter接入已有项目

iOS接入Flutter模块

1.创建Flutter模块

在现有的iOS工程路径下,创建Flutter模块:

cd ~/Work/Projects  // 项目文件夹
flutter create -t module flutter_module

2 创建依赖

  • 在Podfile 文件中添加 Flutter app,在Podfile 文件中添加:
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'xx' do

  use_frameworks!

  # 安装Flutter模块
  install_all_flutter_pods(flutter_application_path)

end

执行 pod install

3.运行

  • 在iOS 项目中,通过 FlutterViewController跳转至Flutter页面

1.创建初始化引擎

#import <Flutter/Flutter.h>

 self.flutterEngine = [[FlutterEngine alloc] initWithName:@"my flutter engine"];
  [self.flutterEngine run];

2.在合适的地方弹出视图

 FlutterEngine *flutterEngine =
        ((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;
    FlutterViewController *flutterViewController =
        [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
    [self presentViewController:flutterViewController animated:YES completion:nil];

Flutter模块调试

这里说一个热重载的方法 (Hot Reload)调试

flutter attach --app-id com.coderwhy.ios-my-test -d 3D7A877C-B0DD-4871-8D6E-0C5263B986CD
  • --app-id后面跟的是 应用的bundId
  • -d 是应用的设备id

可以直接执行 flutter attach 直接调试,但是如果有个多个应用或者设备需要指定对应的设备

接入Flutter Boost

添加 Flutter Boost 依赖

1首先在 Flutter Module 项目中加入 flutter-boost 依赖,即在 pubspec.yaml 文件中的 dev_dependencies 配置增加 flutter-boost 依赖:

dev_dependencies:
  flutter_boost:
     git:
        url: 'https://github.com/alibaba/flutter_boost.git'
        ref: '1.12.13'

2返回iOS工程目录执行pod install

3在AppDelegate.h

#import <Flutter/Flutter.h>

@property (nonatomic,strong) FlutterEngine *flutterEngine;

4在AppDelegate.m

didFinishLaunchingWithOptions方法里

//Flutter 初始化引擎

self.flutterEngine = [[FlutterEngine alloc] initWithName:@”my flutter engine”];

[self.flutterEngine run];

PlatformRouterImp *router = [PlatformRouterImp new];

[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router

​ onStart:^(FlutterEngine *engine) {

​ }];

5在需要的地方进行调用(可以在flutter页面自定义路由跳到指定页面)路由参考:https://www.cnblogs.com/yuyujuan/p/11006780.html

#import <Flutter/Flutter.h>

FlutterViewController flutterViewController = [[FlutterViewController alloc] initWithEngine:AppflutterEngine nibName:nil bundle:*nil];

​ [flutterViewController setInitialRoute:@”route1”];

// [self presentViewController:flutterViewController animated:false completion:nil];

​ [self.rt_navigationController pushViewController:flutterViewController animated:YES];


   转载规则


《Flutter接入已有项目》 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
开放的公共组件 开放的公共组件
1:颜色拾取器
2020-11-18
下一篇 
QMUI-快捷代码片段指南.md QMUI-快捷代码片段指南.md
快捷键汇总NSObject pa - 定义一个 assign 的 property par - 定义一个 assign, readonly 的 property pc - 定义一个 copy 的 property pcr - 定义一个 c
2020-11-12
  目录