1

In my application, I'm downloading and uploading data at some point of time on a separate thread using NSOperation. Both process take approx. 2-3 min. If application remains active then everything works fine. But if the iPad screen is locked (automatically or manually), the app becomes inactive and download/upload get paused and when screen is unlocked after some time process failed with timeout error.

I have "App plays audio" for "Required background modes" in plist file. Do I need to set any other flag for running app while screen is locked?

Please help me what is going wrong.

code007
  • 2,306
  • 2
  • 19
  • 26
Jitendra Singh
  • 2,103
  • 3
  • 17
  • 26

1 Answers1

8

When the screen is locked, your app becomes inactive. Per "Responding to Interruptions" in iOS App Programming Guide:

Pressing the Sleep/Wake button is another type of interruption that causes your app to be deactivated temporarily. When the user presses this button, the system disables touch events, moves the app to the background but sets the value of the app’s applicationState property to UIApplicationStateInactive (as opposed to UIApplicationStateBackground), and finally locks the screen.

You can get extra time for background tasks according to "Executing a Finite-Length Task in the Background" in the same guide:

Apps that are transitioning to the background can request an extra amount of time to finish any important last-minute tasks. To request background execution time, call the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class. If your app moves to the background while the task is in progress, or if your app was already in the background, this method delays the suspension of your app. This can be important if your app is performing some important task, such as writing user data to disk or downloading an important file from a network server.

You can refer to the document to implement a finite-length download task in the background.

Other references:

Community
  • 1
  • 1
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • Thanks for the response. Your answer is perfect but the beginBackgroundTaskWithExpirationHandler: should come if user pressing home button and sending app to the bacground. Why does locking screen doing the same? Isn't app remain in active state when screen is locked? – Jitendra Singh May 08 '12 at 07:07
  • 1
    No, it isn't. Please refer to "Responding to Interruptions" in iOS App Programming Guide at http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html. It says that "Pressing the Sleep/Wake button is another type of interruption that causes your app to be deactivated temporarily. ... **moves the app to the background ... and finally locks the screen.**" – Hailei May 08 '12 at 07:10
  • My app used to work(download/upload), if screen is locked when I build with iOS 4.X but it stopped after I build it with iOS 5 :( – Jitendra Singh May 08 '12 at 07:23
  • 1
    You are right. On iOS 4 the app would only go into the inactive state when the screen was locked, but be moved to the background if the home button was pressed. On iOS 5 when the screen is locked the app is also moved into the background. – Hailei May 08 '12 at 07:38