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

℡╲_俬逩灬. 提交于 2019-12-31 03:01:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!