Search code examples
objective-ctvos

How to send app to background


On tvOS, I've only been able to get the begin states of button presses from the Siri remote by overriding the pressesBegan method on the view. If I use gesture recognizers, it only returns the end state. The catch is, when I override pressesBegan, even if I only use it for the select button, it still overrides the default function of the menu button (to push the app to the background). So I was looking into how to send the app to the background and call that method for the menu button (as is default behavior), but it appears that it is not kosher per Apple's standards to do that.

Here is my code for reference:

-(void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
    for (UIPress* press in presses) {
        switch (press.type) {
            case UIPressTypeSelect:
                NSLog(@"press began");
                break;
            case UIPressTypeMenu:
                // this is where I would call the send to background call if Apple would allow that
                // removing this case also has no effect on what happens
                break;

            default:
                break;
        }

    }

As an alternative, this ONLY sends button release signals, but nothing presses begin.

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTap:)];
tapGesture.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect]];
[view addGestureRecognizer:tapGesture];

Solution

  • When there's some behavior that happens if you don't override a method, and the behavior goes away in an empty override implementation, it stands to reason that behavior is provided by the superclass. (Cocoa is dynamic and complicated, so such inferences aren't true 100% of the time, but often enough.)

    So, just call super for the cases where you don't want your override to change the default behavior:

    case UIPressTypeMenu:
        [super pressesBegan: presses withEvent: event];
        break;