gpt4 book ai didi

ios - 如何正确保存iOS应用状态?

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:50 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

我参加了 WWDC 2012 session ,详细描述了应该如何做到这一点。如果您是注册的 Apple 开发人员,则可以访问 session 中的视频。这个标题为“在 iOS 上保存和恢复应用程序状态”。我发现这是一个关于这个主题的信息量很大的 session 。

关于ios - 如何正确保存iOS应用状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14978056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com