2013-02-20 26 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上保存和恢復應用程序狀態」。我發現這是關於這個主題的非常豐富的會議。

相關問題