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 ...
随机推荐
- Gerrit(0): Install and Config
Install(Ubuntu 17.04): apt-get install default-jre daemon [nginx] #Other OS: install openjdk8-jre [n ...
- css3继承
不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...
- docker mysql 命令导入sql数据文件
1.查看mysql容器 docker ps 比如我的是:94df84cbaaaa 2.复制sql文件到docker mysql里面的tmp文件夹 docker cp ./admin.sql 94df8 ...
- vim对行进行排序
vim自带排序函数sort, 在命令行模式下执行:help sort 可查看其具体用法,摘录如下: Vim has a sorting function and a sorting command. ...
- 68.最大k乘积问题 (15分)
C时间限制:3000 毫秒 | C内存限制:3000 Kb题目内容:设I是一个n位十进制整数.如果将I划分为k段,则可得到k个整数.这k个整数的乘积称为I的一个k乘积.试设计一个算法,对于给定的I和 ...
- 【洛谷】P1229快速幂
题目链接:https://www.luogu.org/problemnew/show/P1226 题意:求b^p % m之后的结果 题解:快速幂模板 代码: #include<iostream& ...
- @ApiImplicitParams、ApiImplicitParam的使用
@ApiImplicitParam:作用在方法上,表示单独的请求参数 参数: 1. name :参数名. 2. value : 参数的具体意义,作用. 3. required : 参数是否必填. 4. ...
- python接口自动化(Cookie_绕过验证码登录)
python接口自动化(Cookie_绕过验证码登录) 有些登录的接口会有验证码,例如:短信验证码,图形验证码等,这种登录的验证码参数可以从后台获取(或者最直接的可查数据库) 获取不到也没关系,可以 ...
- JAVA里面的int类型 和Integer类型,有什么不一样
JAVA里面的int类型 和Integer类型,有什么不一样 原创 2013年09月04日 23:15:11 标签: java / 2120 编辑 删除 JAVA里面的int类型 和Integer类型 ...
- linux 档案 指令(鸟哥私房菜)
[ls -al] [ls -l --full-time] 显示文件完整的日期格式 [ls] 显示非隐藏档的文件信息 [ls -al] 显示所有文档信心.文档前面的 “”.“”表明该文档是隐藏档.eg. ...