2

I've looked around a lot regarding this question but all the ones I've found are pretty outdated.

example

this article is from 2012 and says that it's not possible

Basically I want to be able to start mirroring my iphone through airplay to another screen from within my app and I'm wondering if that's possible now. The articles I've found all say that it isn't possible, or if done, the app won't be accepted for the app store.

Community
  • 1
  • 1
oliver
  • 41
  • 1
  • 2

2 Answers2

4

It's possible to implement own AirPlay picker view as of iOS 11. Please check AVRoutePickerView. It'll allow you to control connections to different AppleTVs/external screens within the app. See example:

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    AVRoutePickerView *routePickerView = [[AVRoutePickerView alloc] initWithFrame:CGRectMake(0.0f, 30.0f, 30.0f, 30.0f)];
    routePickerView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:routePickerView];

    AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:@"https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8"]];
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [playerLayer setFrame:CGRectMake(0.0f, 40.0f, self.view.frame.size.width, self.view.frame.size.height - 40.0f)];
    [self.view.layer addSublayer:playerLayer];
    [player seekToTime:kCMTimeZero];
    [player play];
}

@end

As a result by pressing on AVRoutePickerView you'll get something like this: enter image description here

By pressing on available external screen you'll be able to playback content on it.

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
  • 3
    I've built a small sample using AVRoutePickerView, it seems equivalent to MPVolumeView, it seems to route an existing AV session to another device, but it does not seem to start mirroring for non-media content. – Rafael Nobre Sep 26 '17 at 14:27
  • 2
    Can i trigger selection of a route programmatically using AVRoutePickerView? – Abdul Yasin Nov 15 '17 at 12:30
  • 2
    This answer does not address the question that was asked: the OP wanted to start Airplay *programmatically*, which means that the user shouldn’t have to tap anything on the screen to get it going. – bdesham Oct 03 '18 at 17:10
  • @Maxim, i need to mirroring my app screen to external TV(screen), can I do this by AVRoutePickerView and get connect with a device from list? Or you have idea to do this? I see Google Cast SDK(iOS) only plays video(media content), but I need to mirror a normal app screen/view to an external TV. Anyone can answer, please! – Jamshed Alam May 20 '21 at 08:09
2

It's still not possible. There are no public APIs for enabling AirPlay (and private ones won't be submitted). It's a system level feature that can't be manipulated from an app.

The user must initiate AirPlay, but you could simply display a message suggesting the user turn on AirPlay to enhance the app.

Kenneth
  • 1,035
  • 1
  • 12
  • 26