1

I am developing an application for tvOS. In my app users select their country, states and districts respectively in different view controllers. After selections, App displays a page with the information related to users' selections. When user presses the menu button in this page, I want the app to exit, but the app goes to previous page which is district selection view controller.

How can I make the app exit in this situation. I don't want user to go back all the pages to exit the app.

dilaver
  • 674
  • 3
  • 17

1 Answers1

1

I found the solution from this question that Daniel Storm mentioned. Here is the swift code:

override func viewDidLoad(){
    let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
    tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
    self.view.addGestureRecognizer(tapRecognizer)
}

func handleTap(gesture: UITapGestureRecognizer){
    if gesture.state == UIGestureRecognizerState.Ended {
        let app = UIApplication.sharedApplication()
        app.performSelector(Selector("suspend"))
    }
}

I hope it helps someone.

Community
  • 1
  • 1
dilaver
  • 674
  • 3
  • 17
  • 2
    Keep in mind this is private API (even though using the selector trick) that comes with a risk. If you want your app to suspend by the press of menu button, what should work is to *not* capture the menu button, ie. no gesture recognizer for the menu button, and no raw game controller processing. The default action is then that the system suspends your app. – bio Feb 03 '16 at 11:43