【发布时间】:2011-02-08 00:08:14
【问题描述】:
我有一个应用程序,它将跟踪用户在 iPod 应用程序中所做的一切。为此,我向 NSNotificationCenter 添加了一些观察者,例如 MPMusicPlayerControllerNowPlayingItemDidChangeNotification。但我的问题是,我只有在我的应用程序处于前台时才会收到这些通知,如果它在后台,系统会将通知添加到队列中,然后下次我的应用程序变为活动状态时它会将其传递给我。我对这个队列不感兴趣,因为我想接收实时通知。
即使我的应用处于挂起状态,我有什么方法可以收到这些通知吗?例如,每次收到 NowPlayingItemDidChange 通知时,我只想运行 3 行代码。
这里是我添加观察者的地方。
MPMusicPlayerController *iPodMediaPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver: self selector: @selector(handle_NowPlayingItemChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:iPodMediaPlayer];
[iPodMediaPlayer beginGeneratingPlaybackNotifications];
另外,如果我向观察者添加另一种对象而不是 iPodMediaPlayer,则观察者不会调用该方法。
非常感谢,
阿布拉斯
【问题讨论】:
标签: iphone ios background notifications ipod