Need Docs for Using Airplay as 2nd Screen (not mirroring)

I'm building a SpriteKit game, and would like to use an external display for iOS devices. I've found that using 'AVRoutePickerView' displays the Airplay icon for chosing an external device. However, I am having trouble finding any documentation regarding how to use the choice to present a game scene onto the selected device. Most documentation that I see involves audio only.


I see that I can use 'AVRouteDetector' and 'AVRoutePickerViewDelegate' to respond to changes in the picker, but I don't see any documentation as to how to use either of those two. Purusing the methods and properties of those two classes don't yield anything useful, either.


Is anyone able to either provide an example or point me to where I can find one? I think that if I can have the program recognize when the 2nd display is connected, I could get my game sceen to present onto it.

Accepted Reply

Sorry, looks like I mixed up the capabilities of AirPlay.


There are two capabilities of AirPlay: "media" (music and video) and "external display" (mirroring and custom display). AVRoutePickerView only allows the user to select a device for "media" AirPlay.


However, you want the "external display" capability for your app. The only way for that capability to be enabled is for the user to activate it manually by using the Screen Mirroring setting in Control Center. After searching the web, I did not find a way to show external display options within an app.

Replies

1. Let the user choose the screen with AVRoutePickerView.

2. After the user has chosen the screen, set up a new UIWindow object for the corresponding UIScreen (see section "Handling Screen Connection and Disconnection Notifications" https://developer.apple.com/documentation/uikit/uiscreen)

3. Create a view controller for your content and set it as the UIWindow's rootViewController.

Thanks for the links. They were helpful, but not complete.


I've got an AVRoutePickerView on my screen, and I added an observer to the NotificationCenter (as directed in the documentation). However, when I choose the AppleTV from the view picker, I get the following error message:


MPMediaControlsRemoteViewController Dismissing because view service terminated


I was unable to find anything in a Google search regarding a cause/solution for that message.


However, if I first mirror my iPhone to the AppleTV before launching my app, the observer method does get fired, and I can see the 2nd screen. That's definitely further than I had gotten before, but I'd like to know how to get the AVRoutePickerView observer to work properly. Here are the pertinent parts of my code (a SpriteKit game), in case it is needed for reference:


GameViewController:


override func viewDidLoad() {
    super.viewDidLoad()

    let notifier = NotificationCenter.default
    notifier.addObserver(self, selector: #selector(GameViewController.screenDidConnect), name: NSNotification.Name.UIScreenDidConnect, object: nil)
    notifier.addObserver(self, selector: #selector(GameViewController.screenDidDisconnect), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)

    let skView = self.view as! SKView
    let scene = TitleScene(size: (skView.bounds.size)!)
    scene.backgroundColor = SKColor.black
    skView.presentScene(scene)
}
@objc fileprivate func screenDidConnect(notification: NSNotification) {
    print("GameViewController: Screen was connected: \n\(notification)")
    print("# of Screens: \(UIScreen.screens.count)")
}
   
@objc fileprivate func screenDidDisconnect(notification: NSNotification) {
    print("GameViewController: Screen was disconnected: \n\(notification)")
    print("# of Screens: \(UIScreen.screens.count)")
}



Any ideas as to what I'm missing?

Sorry, looks like I mixed up the capabilities of AirPlay.


There are two capabilities of AirPlay: "media" (music and video) and "external display" (mirroring and custom display). AVRoutePickerView only allows the user to select a device for "media" AirPlay.


However, you want the "external display" capability for your app. The only way for that capability to be enabled is for the user to activate it manually by using the Screen Mirroring setting in Control Center. After searching the web, I did not find a way to show external display options within an app.