问题
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
回答1:
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))
}
}
来源:https://stackoverflow.com/questions/34335505/how-can-i-put-an-apple-tv-app-in-background-after-pressing-the-menu-button