9

I am currently writing a tvOS app. I've been detecting and overriding the menu button with tapRecognizer to switch between storyboards and other functions. My issue is when I am on my home screen and press menu it does not exit the app. Instead it remembers the last function I used when overriding the menu button and performs that function. Any thoughts on how to clear the tapRecognizer? Or a function that will exit the app?

I'm overriding the menu button with

in Storyboard1

tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(home)];
tapRecognizer.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeMenu]];
[self.view addGestureRecognizer:tapRecognizer];

in my home subroutine I send the user back to my home page storyboard. But from then on the menu button will not exit the app but send me back to storyboard1. thanks, SW

Swick
  • 147
  • 1
  • 6
  • This is my code and working fine for me, Try this http://stackoverflow.com/questions/40634638/how-to-handle-menu-button-action-in-tvos-remote/41426885#41426885 – anas.p Jan 02 '17 at 13:09

6 Answers6

14

Instead of using your own gesture recognizer, override pressesBegan:

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
  if(presses.first?.type == UIPressType.Menu) {
    // handle event
  } else {
    // perform default action (in your case, exit)
    super.pressesBegan(presses, withEvent: event)
  }
}
ehynds
  • 4,275
  • 3
  • 21
  • 22
  • Thank you for showing me this method. Were do I but this code? In the viewdidload? – Swick Oct 23 '15 at 17:27
  • That method should be added to your UIViewController – ehynds Oct 23 '15 at 19:21
  • Thank you! Does this mean added it anywhere in my .m file or in the @implementation ViewController{ }? – Swick Oct 25 '15 at 11:27
  • You need something like this when changing scenes or when getting rid of gestures – crashoverride777 Nov 24 '15 at 13:11
  • I am working on Apple Tv application. This app is based on play video. My requirement is that when user pause the video and press the menu button, the video play continuous. I want on video pause state that the user press the menu button then the screen redirect to a previous screen without play video. – Sankalap Yaduraj Singh Jul 19 '16 at 12:18
4

If you are using UIGestureRecognizer instead of responding to presses, all you need to do is to disable the recognizer:

tapRecognizer.enabled = NO;

So if no recognizer with UIPressTypeMenu is listening, tvOS suspends the app and displays the home screen. (I've tested this)

bio
  • 571
  • 2
  • 14
4

You have to override 2 methods to prevent exiting app by pressing Menu button.

Here is ready-to-use template:

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for press in presses {
        switch press.type {
        case .Menu:
            break
        default:
            super.pressesBegan(presses, withEvent: event)
        }
    }
}

override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for press in presses {
        switch press.type {
        case .Menu:
            //Do some staff there!
            self.menuButtonPressed()
        default:
            super.pressesEnded(presses, withEvent: event)
        }
    }
}
skywinder
  • 20,546
  • 15
  • 87
  • 122
2

If you overwrite the menu button, the app won't be accepted:

EDIT: You can overwrite, but the menu button has to work as a back button to homescreen from the entry point of the app.

10.1 Details

The Menu button on the Siri Remote does not behave as expected in your app.

Specifically, when the user launches the app and taps the Menu button on the Siri remote, the app does not exit to the Apple TV Home screen.

Next Steps

Please revise your app to ensure that the Siri remote buttons behave as expected and comply with the Apple TV Human Interface Guidelines.

matthiaswitt
  • 186
  • 9
  • Is this true? I know of a few apps that don't exit when you press the Menu button. I think it makes sense for it to exit to the home screen IF it's at the entry point of the application, ie it's own home screen. But I have seen apps like Hungry Shark Evolution which don't exit when pressing menu button. It's used more as a BACK button in that game. But even on main menu it won't exit back to the Apple TV home screen – prototypical May 10 '16 at 22:10
  • Yes, you are right. But on the entry-ViewController it has to lead back to the home screen, so overwrite it only if neccessary. – matthiaswitt May 27 '16 at 13:44
  • I need to know how to conditionally handle the button press. In other words, how do I exit to the home screen programmatically (ideally without killing the app)? – livingtech Mar 30 '21 at 21:32
0

It may be help you... it is swift code.

let menuPressRecognizer = UITapGestureRecognizer()
menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(_:)))
menuPressRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.hashValue)]
self.view.addGestureRecognizer(menuPressRecognizer)
Sankalap Yaduraj Singh
  • 1,007
  • 1
  • 12
  • 30
0

As per Apple's documentation, for custom press handling, we should override all four of these methods-

- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);

This is the official documentation from XCode:

Generally, all responders which do custom press handling should override all four of these methods.

Your responder will receive either pressesEnded:withEvent or pressesCancelled:withEvent: for each

press it is handling (those presses it received in pressesBegan:withEvent:).

pressesChanged:withEvent: will be invoked for presses that provide an analog value (like thumbsticks or analog push buttons)

*** You must handle cancelled presses to ensure correct behavior in your application. Failure to do so is very likely to lead to incorrect behavior or crashes.

atulkhatri
  • 9,359
  • 3
  • 45
  • 79