Creating a background service on an iPhone
Since iOS 4, Apple has supported background services, which means your apps can now run code in the background, much like Android apps (however, there are some limitations as well as workarounds). In this recipe, we are going to create a background service that will execute a set piece of code from a separate file called bg.js
. We will also log each stage of the background service cycle to the console. Thus, you will understand each part of the process.
How to do it…
Open your project's app.js
file and enter the following text (deleting any code that exists):
//create root window var win1 = Ti.UI.createWindow({ backgroundColor : '#fff', title : 'Background Services' }); //register a background service. //this JS will run when the app is backgrounded var service = Ti.App.iOS.registerBackgroundService({ url : 'bg.js' }); Ti.API.info("registered background service = " + service); //fired when an app is resuming for suspension Ti.App.addEventListener...