实现Flutter跳转iOS原生
一、Flutter如何将消息传递至iOS
1.创建一个通信频道
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
...
class _MyHomePageState extends State<MyHomePage> {
static const platform = const MethodChannel('samples.flutter.dev/goToNativePage');
}
2.实现Trigger Function
Future<void> _goToNativePage() async {
try {
final int result = await platform
.invokeMethod('goToNativePage', {'test': 'from flutter'});
print(result);
} on PlatformException catch (e) {}
}
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
child: Text('去原生界面'),
onPressed: _goToNativePage,
color: Colors.blueAccent,
textColor: Colors.white,
),
Text(
"Flutter 页面",
style: new TextStyle(
fontSize: 30.0,
fontWeight: FontWeight.w900,
fontFamily: "Georgia",
),
)
],
),
),
);
}
添加一个按钮,给这按钮再添加一个_goToNativePage方法,在这里如果还要传递参数的话,直接像这样写就ok了
二、iOS原生接受并跳转
因为你导航到新界面,所以需要引入
UINavigationController
在iOS的AppDelegate.m里
@implementation AppDelegate
文件上方添加代码
@interface AppDelegate()
@property (nonatomic, strong) UINavigationController *navigationController;
@end
1.将FlutterView设为根视图
FlutterViewController *controller = (FlutterViewController*)self.window.rootViewController;
2.嵌入导航堆栈里
self.navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.window makeKeyAndVisible];
3.Flutter和原生通信的接口的实现
FlutterMethodChannel* testChannel =
[
FlutterMethodChannel methodChannelWithName:@"samples.flutter.dev/goToNativePage"
binaryMessenger:controller
];
[testChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
NSLog(@"%@", call.method);
//接收从flutter传递过来的参数
NSLog(@"%@", call.arguments[@"test"]);
if ([@"goToNativePage" isEqualToString:call.method]) {
//实现跳转的代码
} else {
result(FlutterMethodNotImplemented);
}
}];
想获取从flutter传递过来的参数,
call.arguments[@"key"]
4.实现跳转到原生界面
到了这,就相当简单了,补全跳转代码
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"NativeViewController"];
vc.navigationItem.title = call.arguments[@"test"];
[self.navigationController pushViewController:vc animated:true];
Swift :Demon
重点
这篇文章是我用编程界最流行广泛的技术Cmmand+C+V抄的,连作者的名字(進源商贸…)我也直接省略了!你没看错,快去下方评论区用你犀利粗俗的笔锋,站在道德的最高峰无情的谴责我吧! 你写的越多我越高兴。你要还是气不过就来下方的扣群当面找我