3
我正在开发能够在后台播放音乐的音乐播放器应用程序。只要音乐正在播放,应用程序无论如何都不会终止,但如果播放停止并且应用程序处于后台,则可能会终止播放。为了让应用更加人性化我要保存播放队列和状态,当应用程序正在被系统终止了,我实现了在应用程序的委托以下行:如何正确保存iOS应用程序状态?
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
/*
Archive the current queue controller to be able to continue the playback like the app never has been terminated
when user is launching it again.
*/
SMKQueueController *queueController = [[UIApplication sharedApplication] queueController];
[coder encodeObject:queueController forKey:@"queueController"];
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
/*
Restore the recently used queue controller to be able to continue the playback like the app never has been
terminated when use is launching it again.
*/
SMKQueueController *queueController = [coder decodeObjectForKey:@"queueController"];
[[UIApplication sharedApplication] setQueueController:queueController];
return YES;
}
有时候(尤其是当我杀它通过双击家庭按钮菜单手动)它工作,因为我希望它的工作。但是有时这种方法在应用程序被终止时不会被调用。
所以我的问题是:我误解了这些方法的工作原理或它们的用途吗?还是有更好的地方来实现这样的功能?