Sunday, October 14, 2018

My Solution to Background Task Can't be More Than 3 Minutes iOS

If you use UIBackgroundTaskIdentifier for your background task like long download that i did. When the phone is off or the phone is screen lock, the app will crash if it's longer than 3 minutes or limited time. You can find detail on https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html.

This is the explanation from apple why they limit our background task.

Always try to avoid doing any background work unless doing so improves the overall user experience. An app might move to the background because the user launched a different app or because the user locked the device and is not using it right now. In both situations, the user is signaling that your app does not need to be doing any meaningful work right now. Continuing to run in such conditions will only drain the device’s battery and might lead the user to force quit your app altogether. So be mindful about the work you do in the background and avoid it when you can.

This is the code that i wrote to run background mode.


So this is the secret that i did to make background task run more than 3 minutes. It's to keep the view controller active. It means it can't be idle except the user press home or button lock. This is the code that i placed.


Like game download or update in game screen. It never runs when the phone is off or press home to minimize the game. Hope it helps you to do a better plan for your apps.

No comments:

Post a Comment