MainViewController.m 1.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
Ian Hickson's avatar
Ian Hickson committed
3
// found in the LICENSE file.
4 5 6 7 8 9 10 11

#import <Foundation/Foundation.h>

#import "MainViewController.h"
#import "NativeViewController.h"

@interface MainViewController ()

12 13
@property (nonatomic) NativeViewController* nativeViewController;
@property (nonatomic) FlutterViewController* flutterViewController;
14
@property (nonatomic) FlutterBasicMessageChannel* messageChannel;
15 16 17
@end

static NSString* const emptyString = @"";
18
static NSString* const ping = @"ping";
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
static NSString* const channel = @"increment";

@implementation MainViewController

- (NSString*) messageName {
  return channel;
}

- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {

  if ([segue.identifier isEqualToString: @"NativeViewControllerSegue"]) {
    self.nativeViewController = segue.destinationViewController;
    self.nativeViewController.delegate = self;
  }

  if ([segue.identifier isEqualToString:@"FlutterViewControllerSegue"]) {
35 36
    self.flutterViewController = segue.destinationViewController;

37 38 39
    self.messageChannel = [FlutterBasicMessageChannel messageChannelWithName:channel
                                                             binaryMessenger:self.flutterViewController
                                                                       codec:[FlutterStringCodec sharedInstance]];
40 41

    MainViewController*  __weak weakSelf = self;
42
    [self.messageChannel setMessageHandler:^(id message, FlutterReply reply) {
43
      [weakSelf.nativeViewController didReceiveIncrement];
44
      reply(emptyString);
45
    }];
46 47 48 49
  }
}

- (void)didTapIncrementButton {
50
  [self.messageChannel sendMessage:ping];
51 52 53
}

@end