問題描述
我正在開發一個在后臺播放音頻的 iPhone 應用.如果用戶鎖定屏幕,我希望音頻繼續播放,但如果他們決定切換到另一個應用程序(通過按下主頁按鈕),則暫停.
I'm developing an iPhone app that plays audio in the background. I want the audio to keep playing if the user locks the screen, but pause if they decide to switch to another app (by pressing the home button).
在 iOS 4 上沒有問題,因為應用程序會在屏幕鎖定時進入非活動狀態,并且只有在按下主頁按鈕時才會移動到后臺.在 iOS 5 上,當屏幕鎖定時,應用程序現在也被移到后臺,因此似乎不再可能區分這兩種狀態.這個問題有解決辦法嗎?
On iOS 4 there was no problem because the app would go into the inactive state when the screen was locked and only be moved to the background if the home button was pressed. On iOS 5 when the screen is locked the app is now also moved into the background, so it seems it is no longer possible to tell the difference between the two states. Is there a solution to this problem?
推薦答案
您可以通過檢查 UIApplication
的 applicationState
屬性來區分這兩種情況.對于因鎖屏而進入后臺的應用程序,它將設置為 UIApplicationStateInactive
,否則設置為 UIApplicationStateBackground
.
You can distinguish these two cases by checking the applicationState
property of UIApplication
. It will be set to UIApplicationStateInactive
for an application that did enter background because of the lock screen, and to UIApplicationStateBackground
otherwise.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
NSLog(@"Sent to background by locking screen");
} else if (state == UIApplicationStateBackground) {
NSLog(@"Sent to background by home button/switching to other app");
}
}
這篇關于如何區分iOS5上的屏幕鎖定和主頁按鈕按下?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!