FlutterからSwift Kotlinコードを呼ぶ
結構簡単に実装できます。
まずはFlutter側の実装
callNativeComponent(String groupId) async { MethodChannel callNativeViewChannel = const MethodChannel("jp.groupsnap/CreateGroupDetail"); try { await callNativeViewChannel.invokeMethod('callViewController'); } on PlatformException catch (e) { print(e); } }
invokeMethod
を呼ぶだけです。
Swift側の実装
ios/Runner.xcodeproj
をXcodeを開きましょう。
- AppDelegate.swiftを編集します。
let createGroupChannel = FlutterMethodChannel.init(name: "jp.groupsnap/CreateGroupDetail", binaryMessenger: flutterViewController) createGroupChannel.setMethodCallHandler({(call, result) -> Void in // ここでSwiftの処理を実装する })
jp.groupsnap/CreateGroupDetail
のところは好きな文字列を指定して、Swift側に同じ文字列を設定すれば呼ぶことができます。
あとはSwiftでいろいろコード書きましょう。
Kotlin側の実装
android/app/src
を開きます。
- MainActivity.ktを編集します。
// call CreateGroup Activity MethodChannel(flutterView, `jp.groupsnap/CreateGroupDetail`) .setMethodCallHandler { _, _ -> // なんか処理を書く }
あとはいろいろ処理を書きましょう。
公式にも書かれているのでこちらもどうぞ
以前登壇資料も書いたのでどうぞ。