Flutter —— 原生交互
一:传递数据
flutter:
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 中:
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接收消息:
二、页面跳转
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)
}