未调用iOS remoteControlReceivedWithEvent

时间:2012-05-22 18:01:26

标签: iphone ios

在.plist中我添加了法线:http://tinyurl.com/c7e9joy 当iPhone被锁定或我在应用程序之外并打开远程按钮时,我可以看到应用程序徽标和标题,但他们没有反应。 (我正在自己的iPhone上测试。)

// Makes sure we are able to connect to the media buttons on the lock screen.
- (BOOL) canBecomeFirstResponder
{
return YES;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
NSLog(@"REMOTE");
if (receivedEvent.type == UIEventTypeRemoteControl) {

    switch (receivedEvent.subtype) {

        case UIEventSubtypeRemoteControlTogglePlayPause:
            [self playBtnPressed:nil];

            break;

        case UIEventSubtypeRemoteControlPreviousTrack:
            [self skipTrack:nil];
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            [self skipTrack:nil];
            break;

        default:
            break;
    }
}
}

2 个答案:

答案 0 :(得分:2)

您要取消注册viewWillDisapear中的事件,这意味着只要手机上看不到该视图,您就不会注册这些事件。我会更改您注册和取消注册到viewDidLoad和viewDidUnload挂钩的位置。

答案 1 :(得分:2)

您可能还需要添加:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

UIResponder的任何子类都需要实现此功能,否则它将不接受第一响应者状态。