概要

前一篇文章中我们提到,iOS跳转到Flutter工程指定页面时(多个),Flutter只有单例,设置setInitialRouter 无效,如下

  let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!
flutterViewController.setInitialRoute("test1")

基于不是很甘心,一直想实现完美的解决方案,所以最近几天又看了下解决各方面的解决方案,最终还是有了可行方案,步骤如下

1、设置delegate 代码

这里代码 多了 ‘FlutterBasicMessageChannel’  设置,其中_kReloadChannelName要和 flutter上的代码保持一致

let _kReloadChannelName = "reload"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate ,FlutterAppLifeCycleProvider{ static var shared: AppDelegate? var window: UIWindow? var _lifeCycleDelegate = FlutterPluginAppLifeCycleDelegate()
var flutterEngine : FlutterEngine!
var flutterViewController : RKFlutterViewController!
var reloadMessageChannel : FlutterBasicMessageChannel! func addApplicationLifeCycleDelegate(_ delegate: FlutterPlugin) {
_lifeCycleDelegate.add(delegate)
} func flutterSetup(){
flutterEngine = FlutterEngine(name: "rokid.flutter", project: nil)
flutterEngine.run(withEntrypoint: nil)
//全局引擎(解决启动加载时候m,无法处理和native交互问题)
flutterViewController = RKFlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!
GeneratedPluginRegistrant.register(with: flutterEngine)
//实现App 路由跳转
reloadMessageChannel = FlutterBasicMessageChannel(name: _kReloadChannelName, binaryMessenger: flutterEngine, codec: FlutterStringCodec.sharedInstance())
} func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
flutterSetup()
...
}

2、iOS App 跳转指定路由

   @objc func handleButtonAction() {
self.engine().navigationChannel.invokeMethod("setInitialRoute", arguments: "test")
self.reloadMessageChannel().sendMessage("test")
let flutterViewController = FlutterViewController(engine: self.engine(), nibName: nil, bundle: nil)!
self.navigationController?.pushViewController(flutterViewController, animated: true)
} func engine() -> FlutterEngine {
return (UIApplication.shared.delegate! as! AppDelegate).flutterEngine
} func reloadMessageChannel() -> FlutterBasicMessageChannel {
return (UIApplication.shared.delegate! as! AppDelegate).reloadMessageChannel
}

3、flutter路由代码如下

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:ui' as ui;
import 'src/pages/playground/PlaygroundPage.dart'; /// Channel used to let the Flutter app know to reset the app to a specific
/// route. See the [run] method.
///
/// Note that we shouldn't use the `setInitialRoute` method on the system
/// navigation channel, as that never gets propagated back to Flutter after the
/// initial call.
const String _kReloadChannelName = 'reload';
const BasicMessageChannel<String> _kReloadChannel =
BasicMessageChannel<String>(_kReloadChannelName, StringCodec()); void main(){
// Start listening immediately for messages from the iOS side. ObjC calls
// will be made to let us know when we should be changing the app state.
_kReloadChannel.setMessageHandler(run);
// Start off with whatever the initial route is supposed to be.
run(ui.window.defaultRouteName);
} Future<String> run(String name) async{
// The platform-specific component will call [setInitialRoute] on the Flutter
// view (or view controller for iOS) to set [ui.window.defaultRouteName].
// We then dispatch based on the route names to show different Flutter
// widgets.
// Since we don't really care about Flutter-side navigation in this app, we're
// not using a regular routes map.
switch (name) {
case "test":
runApp(appRouter(title: "我是路由测试test00",));
break;
case "test1":
runApp(appRouter(title: "我是路由测试test01",));
break;
case "test2":
runApp(appRouter(title: "我是路由测试test02",));
break;
default:
runApp(MyApp());
break;
}
return '';
} class appRouter extends StatelessWidget {
appRouter({this.title}); final String title;
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter rokid',
debugShowCheckedModeBanner: false,// 显示和隐藏
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: PlaygroundPage(title: '$title'),
);
}
} class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter rokid',
debugShowCheckedModeBanner: false,// 显示和隐藏
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the
// counter didn't reset back to zero; the application is not restarted.
primarySwatch: Colors.blue,
),
home: PlaygroundPage(title: '若琪实验室'),
routes: <String ,WidgetBuilder>{
"router1": (_) => new PlaygroundPage(title: "我是内部路由测试test00",),
"router2": (_) => new PlaygroundPage(title: "我是内部路由测试test01",)
},
);
}
}

思考和总结

上面代码可以实现:native -> 任意 flutter   ,但是flutter Engine是单例子,能否实现 native->flutter ->native->flutter呢?

功能和交互如何去选择呢???

参考资料

https://github.com/flutter/flutter/issues/27882

https://github.com/flutter/flutter/blob/master/dev/integration_tests/ios_add2app/ios_add2app/MainViewController.m

https://github.com/flutter/flutter/blob/master/dev/integration_tests/ios_add2app/flutterapp/lib/main.dart

iOS 工程实现native 跳转指定的Flutter 页面的更多相关文章

  1. silverlight 跳转指定的aspx页面

    1.在xaml.cs中直接访问.并传递参数 System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(HtmlPage.Document.Docu ...

  2. Flutter-现有iOS工程引入Flutter

    前言 Flutter 是一个很有潜力的框架,但是目前使用Flutter的APP并不算很多,相关资料并不丰富,介绍现有工程引入Flutter的相关文章也比较少.项目从零开始,引入Flutter操作比较简 ...

  3. react native 之 在现有的iOS工程中集成react native

    在现有的iOS工程中集成react native, 或者说将react native引入到iOS 项目,是RN和iOS混合开发的必经之路 参考官网教程:https://reactnative.cn/d ...

  4. iOS实现在webview页面内点击链接,跳转指定App

    早上和UI刚谈到这个需求,然后自己试了一下,发现还是蛮简单的,记录一下: 思路分析: iOS内应用之间跳转都会用到 URL Schemes这个东西,简单的讲,这个就是用来定义app身份的一个id识别, ...

  5. iOS 工程自动化 - 思路整理

    4 月份参加 2017@Swift 大会的时候有幸听到了 @zesming 大佬关于美团组件化的 Topic,有一张图印象特别深刻. 来自 @zesming 大佬 后来跟 @zesming 大佬沟通怎 ...

  6. Mac中搭建 iOS 的 React Native 环境

    手把手教你在Mac中搭建iOS的 React Native环境 http://www.cnblogs.com/damnbird/p/6074607.html 准备工作 1.你需要一台Mac电脑..(这 ...

  7. iOS 工程自动化 - OCLint

    前言 最近一直在做 iOS 工程自动化方向的事情,所以把自己研究和实践的内容进行记录并分享,希望能给大家一些帮助. 为什么要使用 OCLint 做为一个静态代码分析工具,我们引入 OCLint 的目的 ...

  8. iOS界面之间的跳转方式

    iOS界面之间的跳转方式基本有3种. .改变window的根视图 [self.window setRootViewController:VC]; .模态弹出 [self presentViewCont ...

  9. Android通过JNI实现守护进程与卸载后跳转指定网页

    JNI进程守护 c代码部分如下:JNIEXPORT void JNICALL Java_com_sharetimes_qude_jni_JNIDaemon_daemon(JNIEnv * env, j ...

随机推荐

  1. Java进程Runtime、Process、ProcessBuilder调用外部程序

    原文地址:https://blog.csdn.net/c315838651/article/details/72085739 通过Java执行系统命令,与cmd中或者终端上一样执行shell命令,最典 ...

  2. 剑指offer——50最长不含重复字符和子字符串

    题目: 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度.假设字符串中只包含’a~z”的字符.例如,在字符串“arabcacfr"中,最长的不含重复字符的子字符串 ...

  3. Red Hat Enterprise Linux 7.7 使用最小化安装后,怎么安装桌面的解决方法

    准备工具: xshell6,xftp6,到官网(https://www.netsarang.com/zh/downloading/)进行下载,教育版的,个人使用 虚拟机安装教程百度即可,安装时有两个重 ...

  4. FP-Tree -关联规则挖掘算法(转载)

    在关联规则挖掘领域最经典的算法法是Apriori,其致命的缺点是需要多次扫描事务数据库.于是人们提出了各种裁剪(prune)数据集的方法以减少I/O开支 支持度和置信度 严格地说Apriori和FP- ...

  5. [BOI2009]Radio Transmission 无线传输

    题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入输出格式 输入格式: 第一行给出字符串的长度,1 < L ≤ 1, ...

  6. 构造+数位dp

    参考博客: 题目链接: 题意:给定正整数a,b,k,你的任务是在所有满足a<=n<=b中的整数n中,统计有多少个满足n自身是k的倍数,且n的各位数字之和也是k的倍数. [思路] 这种题的固 ...

  7. JS设置浏览器缓存,以及常用函数整理

    //设置缓存 function set_cache(key,value){ if(key=='') return false; localStorage.setItem(key, value); } ...

  8. AutoFac mvc和WebAPI 注册Service (接口和实现)

    AutoFac  mvc和WebAPI  注册Service (接口和实现) 1.准备组件版本:Autofac 3.5.0    Autofac.Integration.Mvc 3.3.0.0  (I ...

  9. PROJECT | 四则运算UI设计 - 项目总结

    [项目Github地址] https://github.com/oTPo/hw2 [项目规划] PSP表格 事项 预计时间(min) 实际花费时间(min) 需求分析 60 60 开发流程分析 30 ...

  10. projects

    layout title project 开源项目 本文记录我收藏的开源项目