5

The desired output looks like this,

desired output

how to show this on click event programmatically using flutter(even with native code if possible), it's really appreciated if anyone could show an example. If there is no direct approach to this then a platform specific example using MethodChannel is also very welcome. Native code example must be in Objective C.

Additionally I have tried to use flutter_to_airplay but project fails to run and also has other functionalities that are not needed in this context, what is needed is showing Airplay panel only.

(Answer by M123 native code completely not working)

user6597752
  • 170
  • 1
  • 18

1 Answers1

2

Here is a example how to open the AirPlayPanel in objective c.

Setup flutter

First you have to create a channel. To start communicating to the native code.

All channel names used in a single app must be unique; prefix the channel name with a unique ‘domain prefix

static const platform = const MethodChannel('stack.M123.dev/airplay');

Then you have to invoke a method on the method channel.

  Future<void> _openAirPlay() async {
    try {
      await platform.invokeMethod('openAirPlay');
    } on PlatformException catch (e) {
      print('error');
    }


  }

Native part

Now the flutter part is done.

First you have to add suppport for swift. For this open the ios folder in the flutter root with XCode.

Expand Runner > Runner in the Project navigator.

Open the AppDelegate.m located under Runner > Runner in the Project navigator.

Create a FlutterMethodChannel and add a handler inside the application didFinishLaunchingWithOptions: method. Make sure to use the same channel name as was used on the Flutter client side.

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

@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
  FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;

  FlutterMethodChannel* airPlayChannel= [FlutterMethodChannel
                                          methodChannelWithName:@"stack.M123.dev/airplay"
                                          binaryMessenger:controller.binaryMessenger];

  [airPlayChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
    // Note: this method is invoked on the UI thread.
      if ([@"getBatteryLevel" isEqualToString:call.method]) {
int returnValue = [weakSelf openAirPlay];


  result(@(returnValue));

  }];

  [GeneratedPluginRegistrant registerWithRegistry:self];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

Add the method in the AppDelegate class, just before @end

- (int)openAirPlay {
    //Open air play
    return (int)(100);
  
}

Discalimer: I am not a IOS developer so the steps are mostly theoretical. I am following the official guide from Flutter. It can be found in full length here.

m123
  • 2,824
  • 1
  • 11
  • 29
  • thanks for the detailed example but as I mentioned in the question `platform specific example (in objective c) ` my iOS side of the code is objective c and your example is coded in swift, can you edit your example with objective c ? I updated my question to make it more clear it must be in objective c. – user6597752 Jan 28 '21 at 05:59
  • @user6597752 Updated the native part to objective c. Hope this helps, but I can't confirm, that it works. – m123 Jan 29 '21 at 11:16
  • thanks again but please the syntax isn't even correct (the objc) I couldn't run your example on ios, can you please try running your example ? I cant correct it bcoz i hv no idea about objective c – user6597752 Jan 30 '21 at 18:53
  • @user6597752 sorry, like I said, I am not a IOS developer myself, and I have no possebility to test the native code, since I am a Windows user. Please try to follow the official guide I linked in the answer – m123 Jan 31 '21 at 14:44
  • The official guide is about connecting the flutter code with native code and that has been done, my issue is with the code that opens the airplay panel, there no official guide that i can follow which shows how to open airplay panel using obj c, is there ? – user6597752 Feb 04 '21 at 05:19