I have this in the AppDelegate (didFinishLaunching):
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
I tried handling the events in the relevant view controllers but that was spotty (some view controllers would get the events and others wouldn't, even when they were first responders). I tried subclassing UIApplication. That didn't work. Now I'm trying to subclass UIWindow and do this (see the comments):
- (void)sendEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
NSLog(@"I wish this happened"); //this never happens
}
else
{
NSLog(@"some other event"); //this does happen on any other interaction
[super sendEvent:event];
}
}
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
NSLog(@"got remote event"); // this never happens either
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
{
NSLog(@"handle play/pause");
break;
}
default:
break;
}
}
}
I have tried both with and without this in the info plist:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
Doesn't make a difference. As the Apple docs state, you can simulate remote control events with the audio controls (double tap home button and scroll to them on the bottom). When I press play or pause, it just plays audio from my iTunes library. I have tried the buttons on the Apple headphones as well. Nothing.
All I want to do is detect the play/pause button on a remote control, and handle the event. What else do I need to do to catch these events?