skip to Main Content

so i have an issue when trying to implement the platform method channel for my flutter project, When i try to invoke a method from flutter to ios side, it gets triggered and everything is working perfectly but when i try to invoke a method from ios side (appDelegate file) to flutter in order to perform a sepecific task, it’s not working.

AppDelegate.swift Code:

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    application.registerForRemoteNotifications()
    GeneratedPluginRegistrant.register(with: self)

    let controller = (window?.rootViewController as! FlutterViewController)
        let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
        methodChannle.invokeMethod("taskName", arguments: [:])
      
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

Flutter main.dart:

const methodChannel = MethodChannel('channelMethodTest');

Future<void> methodHandler(MethodCall call) async {
  final String idea = call.arguments;

  switch (call.method) {
    case "taskName":
      print(
          "receiving task from ios to flutter");
      break;
    default:
      print('no method handler for method ${call.method}');
  }
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  methodChannel.setMethodCallHandler(methodHandler);
}

I tried searching for tutos on how to do it but i cannot find any

2

Answers


  1. Ensure that the channel name is the same in Flutter and iOS side. Now they seem to be different:

    let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
    
    const methodChannel = MethodChannel('beyang.com-channelMethodTest');
    
    Login or Signup to reply.
  2. you need to make the channelmethod var global then you need to instantiate the method channel in the didFinishLaunchingWithOptions func if you’re using the method channel to invoke any method in the app deletage or any extension related to it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search