按下“菜单”按钮后,我尝试使用私有方法将应用程序置于后台;并且以下代码正常工作:
@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)];
}
}
但我希望在不使用私有方法的情况下获得相同的结果"暂停"因为Apple可能因使用私有方法而拒绝该应用程序。
[app performSelector:@selector(suspend)];
任何建议都将受到赞赏
答案 0 :(得分:1)
Swift 3解决方案:
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))
}
}