2013-02-20 88 views
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; 
} 

有时候(尤其是当我杀它通过双击家庭按钮菜单手动)它工作,因为我希望它的工作。但是有时这种方法在应用程序被终止时不会被调用。

所以我的问题是:我误解了这些方法的工作原理或它们的用途吗?还是有更好的地方来实现这样的功能?

回答

2

我参加了一个WWDC 2012会议,描述了应该如何完成这个任务。如果您是注册的Apple开发人员,则可以从会话中访问视频。这一个标题为“在iOS上保存和恢复应用程序状态”。我发现这是关于这个主题的非常丰富的会议。

相关问题