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 ...
随机推荐
- Leetcode刷题——007.整数反转
上代码: #include <cmath> class Solution { public: int reverse(int x) { ; long long tx=llabs(x); ) ...
- python--接口类与抽象类
一. 继承有两种用途: """ 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了 ...
- 从零开始搭建系统1.4——MySql安装及配置
安装环境:CentOS7 64位 ,安装MySQL5.7 1.创建mysql目录 2.在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...
- linux 7 添加永久路由方法
linux 7 添加永久路由 用route命令添加 仅仅是当前状态下生效,一旦重启就会失效. 所以要在/etc/sysconfig/network-scripts/这个路径下添加一个文件route-{ ...
- 创建maven项目的时候:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any of the configured repositories. 解决办法
问题: https://yq.aliyun.com/ziliao/364921 尝试没成功. https://www.aliyun.com/jiaocheng/296712.html 尝 ...
- zmq中的router和dealer
https://segmentfault.com/q/1010000000638839 在zeromq的guide里,它用router/dealer模式做了一个broker client对应ZMQ_R ...
- CSIC_716_20191203【 socket网络编程,以及沾包问题的高级解决方式】
AF_UNIX(本机通信) AF_INET(TCP/IP – IPv4) AF_INET6(TCP/IP – IPv6) SOCK_STREAM(TCP流) SOCK_DGRAM(UDP数据报) 远程 ...
- 安装percona-toolkit.rpm时候报错:perl(Time::HiRes) is needed by percona-toolkit-2.2.16-1.noarch
1.安装percona-toolkit.rpm时候报错: warning: percona-toolkit.rpm: Header V4 DSA/SHA1 Signature, key ID cd2e ...
- 区别 |python |[-1]、[:-1]、[::-1]、[2::-1]的使用
格式 list[start :end :方向] start——>开始下标位置 end——>结束下标位置 方向——> 读取方向.默认正向,-1表示反方向读取 如: import num ...
- 高危预警| SQL数据库成主要攻击对象,或引发新一轮大规模勒索
近日,阿里云安全团队发现,目前互联网上的服务器,SQL数据库仍然有不少处于直接暴露在公网的状态,且数量有上升趋势.黑客可以利用数据库存在的漏洞或弱口令直接获取数据,并植入勒索和挖矿病毒寻求牟利.阿里云 ...