zsong zsong - 1 year ago 72
Swift Question

Allow Siri Remote Menu button when Play/Pause button is overridden

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainController.tapped(_:)))
tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)]

This code allows me to override the play/pause button and it works correctly. However, now I have to long press the Menu button to return to the Apple TV OS menu.

Is there anyway when the Menu button is pressed, it returns directly to the OS menu while the Play/Pause button keeps doing my current logic? I am afraid if clicking on Menu doesn't return to OS menu, my app could be rejected.


Answer Source

To return to the Apple TV home screen you can setup a UITapGestureRecognizer in your viewDidLoad like so:

// Setup Menu Button recognizer
let menuGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleMenuGesture(_:)))
menuGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]

and then in handleMenuGesture you suspend your application:

// MARK: - Handle Siri Remote Menu Button
func handleMenuGesture(tap: UITapGestureRecognizer) {
    print("Menu Gesture")
    UIControl().sendAction(#selector(NSURLSessionTask.suspend), to: UIApplication.sharedApplication(), forEvent: nil)

Related: Siri Remote's Menu Button not exiting application when UIButton is focused