【问题标题】:How to correctly save iOS application state?如何正确保存 iOS 应用程序状态?
【发布时间】:2013-02-20 10:53:44
【问题描述】:

我正在开发一个能够在后台播放音乐的音乐播放器应用程序。只要音乐正在播放,应用程序就不会被终止,但如果播放停止并且应用程序处于后台,它可能会被终止。为了让应用程序更加用户友好,我想在应用程序被系统终止时保存播放队列和状态,因此我在应用程序委托中实现了以下几行:

- (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;
}

有时(尤其是当我通过双击主页按钮菜单手动杀死它时)它会像我期望的那样工作。但有时在应用程序终止时不会调用此方法。

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

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:

    我参加了一个 WWDC 2012 会议,该会议准确地描述了应该如何做到这一点。如果您是注册的 Apple 开发人员,则可以访问会议中的视频。这篇文章的标题是“在 iOS 上保存和恢复应用程序状态”。我发现这是一次关于这个主题的内容非常丰富的会议。

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 2012-03-08
      • 2012-06-11
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多