How to manage two FlutterEventChannel in Objective - C

552

SOLUTION

In this case, I need to use two implementations separately in AppDelegate.m :

#import "AppDelegate.h"
#import <Flutter/Flutter.h>
#import "GeneratedPluginRegistrant.h"

FlutterEventSink eventSinkFirst;
FlutterEventSink eventSinkSecond;

@implementation AppDelegate {

}

- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

    [GeneratedPluginRegistrant registerWithRegistry:self];

    FlutterViewController* controller =
    (FlutterViewController*)self.window.rootViewController;

    FirstStreamHandler* firstStreamHandler =
        [[FirstStreamHandler alloc] init];
    FlutterEventChannel* firstEventChannel =
        [FlutterEventChannel eventChannelWithName:@"my_first_event_channel"
                                  binaryMessenger:controller];
    [firstEventChannel setStreamHandler:firstStreamHandler];


    SecondStreamHandler* secondStreamHandler =
        [[SecondStreamHandler alloc] init];
    FlutterEventChannel* secondEventChannel =
        [FlutterEventChannel eventChannelWithName:@"my_second_event_channel"
                                  binaryMessenger:controller];
    [secondEventChannel setStreamHandler:secondStreamHandler];

    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end


@implementation FirstStreamHandler
- (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)eventSink {
    eventSinkFirst = eventSink;
  return nil;
}

- (FlutterError*)onCancelWithArguments:(id)arguments {
    eventSinkFirst = nil;
  return nil;
}
@end


@implementation SecondStreamHandler
- (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)eventSink {
    eventSinkSecond = eventSink;
  return nil;
}

- (FlutterError*)onCancelWithArguments:(id)arguments {
    eventSinkSecond = nil;
  return nil;
}
@end

And declare in AppDelegate.h :

#import <Flutter/Flutter.h>
#import <UIKit/UIKit.h>

@interface AppDelegate : FlutterAppDelegate<MyCustomDelegates>
@end

@interface FirstStreamHandler : NSObject <FlutterStreamHandler>
@end

@interface SecondStreamHandler : NSObject <FlutterStreamHandler>
@end
Share:
552
Álvaro Agüero
Author by

Álvaro Agüero

Updated on December 14, 2022

Comments

  • Álvaro Agüero
    Álvaro Agüero 11 months

    I have a Flutter app and I need connect with native functions with FlutterEventChannel and for a clean code and ordinate, I need two FlutterEventChannel. The question is : How to determinate when one event is called, It works with one event but I don't know how to do with 2 events. This is my AppDelegate.m

    #import "AppDelegate.h"
    #import <Flutter/Flutter.h>
    #import "GeneratedPluginRegistrant.h"
    
    @implementation AppDelegate {
        FlutterEventSink _eventSink;
    }
    
    - (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
    
        [GeneratedPluginRegistrant registerWithRegistry:self];
        FlutterViewController* controller =
        (FlutterViewController*)self.window.rootViewController;
    
        FlutterEventChannel* eventFirst = [FlutterEventChannel
            eventChannelWithName:@"my_first_event_channel"
                 binaryMessenger:controller];
        [eventFirst setStreamHandler:self];
    
        FlutterEventChannel* eventSecond = [FlutterEventChannel
            eventChannelWithName:@"my_second_event_channel"
                 binaryMessenger:controller];
        [eventSecond setStreamHandler:self];
    
        return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    
    - (FlutterError*)onListenWithArguments:(id)arguments
                                 eventSink:(FlutterEventSink)eventSink {
      _eventSink = eventSink;
      return nil;
    }
    
    - (FlutterError*)onCancelWithArguments:(id)arguments {
      [[NSNotificationCenter defaultCenter] removeObserver:self];
      _eventSink = nil;
      return nil;
    }
    
    @end
    

    I want to know how to active both eventFirst and eventSecond and difference them