Since iOS 7 you only have 180 seconds of background time, but when you press the home button, you do not force the application to run in the background. You are creating a suspend application in the background. Your code does not run when you are in the background.
According to Apple :
When the user is not actively using your application, the system moves it to the background state. For many applications, the background state is just a brief stop on the way to pausing the application. Pausing applications is a way to increase battery life, which also allows the system to allocate important system resources for a new foreground application that has attracted the attention of users.
Applications need special permission to run in the background in order to perform certain limited actions, such as location, audio or Bluetooth, which will keep it in the background.
There is a trick that you can use, but you don’t allow the application for more than ~ 180 s , just implement the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: method as usual and put the following in AppDelegate :
var backgroundUpdateTask: UIBackgroundTaskIdentifier = 0 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { return true } func applicationWillResignActive(application: UIApplication) { self.backgroundUpdateTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ self.endBackgroundUpdateTask() }) } func endBackgroundUpdateTask() { UIApplication.sharedApplication().endBackgroundTask(self.backgroundUpdateTask) self.backgroundUpdateTask = UIBackgroundTaskInvalid } func applicationWillEnterForeground(application: UIApplication) { self.endBackgroundUpdateTask() }
After 180 seconds, the timer will not start anymore, when the application returns to the foreground, it will start to light up again.
I highly recommend you read Apple's Background Execution tutorial, you can know how it works and fill your needs. Hope this helps you.
source share