你想听-(void)remoteControlReceivedWithEvent:(UIEvent *)event
做一个基础UIViewController
,让我们调用它BaseViewController
并添加以下内容:
-(void)remoteControlReceivedWithEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
switch(event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
break;
case UIEventSubtypeRemoteControlPlay:
break;
case UIEventSubtypeRemoteControlPause:
break;
case UIEventSubtypeRemoteControlStop:
break;
default:
break;
}
}
else{
[super remoteControlReceivedWithEvent:event];
}
}
您还希望在基类中包含以下内容,以便您可以实际接收任何远程控制事件。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(beginReceivingRemoteControlEvents)]) {
[application beginReceivingRemoteControlEvents];
}
[self becomeFirstResponder];
}
最后让所有 UIViewController 成为 BaseViewController 的子类。