Genar
Genar

Reputation: 735

How can I put an Apple TV app in background after pressing the Menu button

I have tried to use a private method to put the app in background after pressing the Menu button; and the following code works properly:

 @implementation ViewController {
     UITapGestureRecognizer *tapRecognizer;
  }

 -(void)viewDidLoad {
     [super viewDidLoad];

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

 -(void)handleTap:(UITapGestureRecognizer *)sender {
     if (sender.state == UIGestureRecognizerStateEnded) {
         NSLog(@"Menu button released");
         UIApplication *app = [UIApplication sharedApplication];
         [app performSelector:@selector(suspend)];
     }
 }

But I would like to get the same result without using the private method "suspend" because Apple could reject the app due to using a private method.

 [app performSelector:@selector(suspend)];

Any suggestion will be appreciated

Upvotes: 3

Views: 1211

Answers (1)

Justin Vallely
Justin Vallely

Reputation: 6089

Swift 3 solution:

override func viewDidLoad() {
    super.viewDidLoad()

    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(gesture:)))
    tapRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
    self.view.addGestureRecognizer(tapRecognizer)
}

func handleTap(gesture: UITapGestureRecognizer){
    if gesture.state == UIGestureRecognizerState.ended {
        let app = UIApplication.shared
        app.perform(#selector(URLSessionTask.suspend))
    }
}

Upvotes: 3

Related Questions