Flutter混合开发(二):iOS项目集成Flutter模块详细指南

Build Settings->Build Options->Enable Bitcode

flutter以前的版本是需要添加build phase以构建Dart代码,但是最新的版本已经不需要添加了,可以自动构建。

调用Flutter module

===================================================================================

Flutter为我们提供了两种调用方式:FlutterViewController和FlutterEngine,FlutterEngine在使用的时候会有一些问题,将在下文进行说明。

FlutterViewController方式:

我们打开ViewController.m文件,在里面添加一个加载flutter页面的方法并且添加一个按钮看来调用:

#import “ViewController.h”

#import <Flutter/Flutter.h>

#import “AppDelegate.h”

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {

[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button addTarget:self action:@selector(handleButtonAction) forControlEvents:UIControlEventTouchUpInside];

[button setTitle:@“加载Flutter” forState:UIControlStateNormal];

[button setBackgroundColor:[UIColor blueColor]];

button.frame = CGRectMake(100, 100, 160, 60);

[self.view addSubview:button];

}

  • (void)handleButtonAction{

FlutterViewController *flutterViewController =[FlutterViewController new];

//设置路由参数

[flutterViewController setInitialRoute:@“route2”];

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

}

@end

当我们运行项目点击加载Flutetr按钮时,将会调用Flutter页面。和Android项目集成一样,这里的setInitialRoute可以设置一个json数组来传递需要交互的参数。并在Flutter中使用window.defaultRouteName来获取传递的参数。

FlutterEngine方式:

我们需要在AppDelegate中对FlutterEngine进行初始化。打开AppDelegate.h文件:

#import <UIKit/UIKit.h>

#import <Flutter/Flutter.h>

@interface AppDelegate : FlutterAppDelegate

@property (nonatomic,strong) FlutterEngine *flutterEngine;

@end

在打开AppDelegate.m文件:

// 如果你需要用到Flutter插件时

#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>

#include “AppDelegate.h”

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.flutterEngine = [[FlutterEngine alloc] initWithName:@“io.flutter” project:nil];

[self.flutterEngine runWithEntrypoint:nil];

[GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine]; //如果你需要用到Flutter插件时

return [super application:application didFinishLaunchingWithOptions:launchOptions];

}

@end

然后在ViewController.m文件定义的handleButtonAction中调用:

  • (void)handleButtonAction{

FlutterEngine *flutterEngine = [(AppDelegate *)[[UIApplication sharedApplication] delegate] flutterEngine];

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

[flutterViewContr

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值