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;
}
有時候(尤其是當我殺它通過雙擊家庭按鈕菜單手動)它工作,因爲我希望它的工作。但是有時這種方法在應用程序被終止時不會被調用。
所以我的問題是:我誤解了這些方法的工作原理或它們的用途嗎?還是有更好的地方來實現這樣的功能?