iOS 工程实现native 跳转指定的Flutter 页面
概要
在前一篇文章中我们提到,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
iOS 工程实现native 跳转指定的Flutter 页面的更多相关文章
- silverlight 跳转指定的aspx页面
1.在xaml.cs中直接访问.并传递参数 System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(HtmlPage.Document.Docu ...
- Flutter-现有iOS工程引入Flutter
前言 Flutter 是一个很有潜力的框架,但是目前使用Flutter的APP并不算很多,相关资料并不丰富,介绍现有工程引入Flutter的相关文章也比较少.项目从零开始,引入Flutter操作比较简 ...
- react native 之 在现有的iOS工程中集成react native
在现有的iOS工程中集成react native, 或者说将react native引入到iOS 项目,是RN和iOS混合开发的必经之路 参考官网教程:https://reactnative.cn/d ...
- iOS实现在webview页面内点击链接,跳转指定App
早上和UI刚谈到这个需求,然后自己试了一下,发现还是蛮简单的,记录一下: 思路分析: iOS内应用之间跳转都会用到 URL Schemes这个东西,简单的讲,这个就是用来定义app身份的一个id识别, ...
- iOS 工程自动化 - 思路整理
4 月份参加 2017@Swift 大会的时候有幸听到了 @zesming 大佬关于美团组件化的 Topic,有一张图印象特别深刻. 来自 @zesming 大佬 后来跟 @zesming 大佬沟通怎 ...
- Mac中搭建 iOS 的 React Native 环境
手把手教你在Mac中搭建iOS的 React Native环境 http://www.cnblogs.com/damnbird/p/6074607.html 准备工作 1.你需要一台Mac电脑..(这 ...
- iOS 工程自动化 - OCLint
前言 最近一直在做 iOS 工程自动化方向的事情,所以把自己研究和实践的内容进行记录并分享,希望能给大家一些帮助. 为什么要使用 OCLint 做为一个静态代码分析工具,我们引入 OCLint 的目的 ...
- iOS界面之间的跳转方式
iOS界面之间的跳转方式基本有3种. .改变window的根视图 [self.window setRootViewController:VC]; .模态弹出 [self presentViewCont ...
- Android通过JNI实现守护进程与卸载后跳转指定网页
JNI进程守护 c代码部分如下:JNIEXPORT void JNICALL Java_com_sharetimes_qude_jni_JNIDaemon_daemon(JNIEnv * env, j ...
随机推荐
- 改变IntelliJ IDEA 中的system和config/plugins的默认C盘的路径
1,问题,在为idea在线安装插件时,如JProfiler,会默认安装到C盘,而本人则是希望安装到软件所在的D盘目录下,那么如何修改呢: C:\Users\xxx\.IntelliJIdea\conf ...
- ping命令的应用
Ping命令是工作在 TCP/IP网络体系结构中应用层的一个服务命令, 主要功能是向特定的目的主机发送 ICMP(Iternet Control Message Protocol 因特网报文控制协议) ...
- centos7升级php5.4到php5.6
history命令历史 8 yum provides php #自带的只有5.4版本 9 rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-relea ...
- python去除rpm仓库中同名低版本的包
编程思路1 遍历目标路径的rpm包并保存特性包列表: 2 利用python模块rpmUtils提取RPM包的特征信息:包名 版本号 架构 3 遍历特性列表中存在重复包名的rpm, 将低版本的rpm包 ...
- 2019基于python的网络爬虫系列,爬取糗事百科
**因为糗事百科的URL改变,正则表达式也发生了改变,导致了网上许多的代码不能使用,所以写下了这一篇博客,希望对大家有所帮助,谢谢!** 废话不多说,直接上代码. 为了方便提取数据,我用的是beaut ...
- 【Codeforces】450 B(div2)
题目链接:http://codeforces.com/problemset/problem/450/B 题意: 求这个的第n项. 题解:$f_{i+1} = f_i - f_{i-1} $ \begi ...
- d3操作svg路径动画,及dom移动
图片跟随路径循环运动,dom也跟着路径运动(利用实时获取坐标位置的方法,改变transform) 1,准备路径 a,自己脑补路径 b,在ps上画好,然后在保存成png-24图片,背景透明,在网站htt ...
- ES6点点点运算符
1. rest(可变)参数 * 用来取代arguments 但比arguments灵活,只能是最后部分形参参数 function add(...values) { let sum = ; for(va ...
- Ubuntu's Software
(1)indicator-sysmonitor & acpi (2)nvidia-prime (3)sogou (4)wps (5)ubuntu-tweak
- Json数据交换一Gson
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. 添加依赖 <depe ...