Flutter —— 原生交互

turboksFlutter97

一:传递数据

flutter:


var methodChannel = const MethodChannel("ksapp");


onPressed: () {
// sendToAndroid();
sendToiOS();
},


sendToiOS() async {
try {
    String res =
    await methodChannel.invokeMethod("sendToiOS", '从 flutter 到 iOS');
    print(res);
    setState(() {
    eventt = res;
});
} catch (e) {
    print(e.toString());    
}
}


iOS端接收消息:


AppDelegate.swift 中:

        let controller = self.window.rootViewController


        let channel = FlutterMethodChannel.init(name: "ksapp", binaryMessenger: controller as! FlutterBinaryMessenger)

        channel.setMethodCallHandler { (call, result) in

            self.resultChannel = result

            if call.method == "sendToiOS" {

               //做一些处理

               //。。。。。。

            }

        }

        GeneratedPluginRegistrant.register(with: self)

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)


安卓端接收消息:

MainActivity.kt 中:

class MainActivity: FlutterActivity() {

var CHANNEL: String = "ksapp"
    lateinit var methodChannel: MethodChannel
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
            methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger,CHANNEL)
            methodChannel.setMethodCallHandler { call, result ->
                if (call.method.equals("sendToAndroid")){
                //进行处理
                }
        }
    }
}




iOS主动发起消息:


    lazy var messageChannel:FlutterBasicMessageChannel  = {

        let navigation = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController

        guard let contoller = navigation?.viewControllers.first as? FlutterViewController else {

            fatalError("contoller is not type FlutterViewController")

        }

        

        let channel = FlutterBasicMessageChannel(name: "iostoflutter", binaryMessenger: contoller.binaryMessenger,codec: FlutterJSONMessageCodec.sharedInstance());

        return channel

    }()



        messageChannel.sendMessage("datadatadata")


flutter接收消息:

void initState() {
    super.initState();
    zhuceMessage();
}


zhuceMessage() {
    messageChannel.setMessageHandler((message) async {
        print("$message");
    setState(() {
        eventt = message.toString();
    });
        return "";
    });
}



二、页面跳转

        let controller = self.window.rootViewController

        self.navigationComtroller = UINavigationController.init(rootViewController: controller!)

        window = UIWindow(frame: UIScreen.main.bounds)

        window.rootViewController = self.navigationComtroller

        self.navigationComtroller?.delegate = self

        window.makeKeyAndVisible()

        

        let channel = FlutterMethodChannel.init(name: "ksapp", binaryMessenger: controller as! FlutterBinaryMessenger)

        

        channel.setMethodCallHandler { (call, result) in

            self.resultChannel = result

            if call.method == "sendToiOS" {

                //获取当前位置

                let kspage = KSMapController()

                kspage.block = {newstr in

                    print(newstr)

                    result(newstr)

                }

                self.navigationComtroller?.pushViewController(kspage, animated: true)

            }

        }

        GeneratedPluginRegistrant.register(with: self)

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)

    }

    

    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

        //flutter页面需要隐藏导航

        navigationController.navigationBar.isHidden = viewController.isKind(of: FlutterViewController.self)

    }







相关文章

Flutter —— 使用RGB颜色

Flutter —— 使用RGB颜色

import 'dart:ui';extension HexColor on Color {  /// String is in the format "aabbc...

Flutter —— 轮播图

Flutter —— 轮播图

1.pubspec.yaml  flutter_swiper: ^1.1.6 # 轮播图2.自定义一个类import 'package:flutter/material.dart&#...

Flutter —— 路由

Flutter —— 路由

1. pubspec.yamlfluro: ^1.5.1 #路由2. 路由管理中心    // ignore: import_of_legacy_library...

Flutter —— TabBarView每次进入都刷新?

Flutter —— TabBarView每次进入都刷新?

import 'package:flutter/material.dart';class PageOne extends StatefulWidget {const PageOne({...

Flutter —— 网络请求

Flutter —— 网络请求

1.pubspec.yamldio: ^3.0.9 #网络请求2.使用    var url = "urlurlurlurlurl";   ...

Flutter —— 左右切换视图

Flutter —— 左右切换视图

import 'package:flutter/material.dart';import 'package:ks/pageone.dart';import '...