Flutter跳转iOS原生

实现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抄的,连作者的名字(進源商贸…)我也直接省略了!你没看错,快去下方评论区用你犀利粗俗的笔锋,站在道德的最高峰无情的谴责我吧! 你写的越多我越高兴。你要还是气不过就来下方的扣群当面找我
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值