2

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

Genar
  • 725
  • 1
  • 10
  • 27
  • Could you edit your question to include an example? I'm not sure I entirely understand what you're asking and wanting to achieve. – Daniel Storm Dec 17 '15 at 14:50
  • It is possible to create a simple view controller and then copy the aforementioned code in order to check that it works properly; however I would like to get the same result without using the private method "suspend". Thanks in advance. – Genar Dec 17 '15 at 15:05
  • Doesn't the menu button suspend the app automatically? – Daniel Storm Dec 17 '15 at 16:27
  • Hello Daniel, what happen is that the Menu button goes to the previous screen view controller, and I think this is the default behaviour; however, in some cases (view controllers) I don't want the app go back; instead I want the app go to background. – Genar Dec 17 '15 at 16:39
  • I'd continue with using the private method to suspend the application or rework your applications flow. I doubt Apple would reject your application, but users may think the application has crashed when they are used to the menu button taking them back to the previous screen and not the home screen. – Daniel Storm Dec 18 '15 at 10:28
  • 1
    Thanks Daniel, finally I have decided to rework the app flow. – Genar Dec 18 '15 at 12:26
  • Take a look at my solution here: http://stackoverflow.com/a/34573586/3643020 – Campbell_Souped Jan 10 '16 at 23:14

1 Answers1

2

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))
    }
}
Justin Vallely
  • 5,932
  • 3
  • 30
  • 44
  • 1
    Why would you use URLSession suspend? You should use `NSXPCConnection.suspend`... – Dominik Bucher Nov 23 '17 at 13:29
  • This is just a Swift port of the code in the question, which uses a private method and might get your app rejected. (In that sense, it's not really an answer to this question, which was “how do I do this without a private method?”) – Lynn May 14 '20 at 23:36