Completing the Background Task

After a background task ID has been obtained, the actual work to be done in the background can commence. In the performBackgroundTask method some variables are set up to know where to start counting, to keep count of iterations, and to know when to stop. A reference to NSUserDefaults standardUserDefaults is established to retrieve the last counter used, and store the last counter with each iteration.

NSInteger counter = 0;NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];NSInteger startCounter =[userDefaults integerForKey:kLastCounterKey];NSInteger twentyMins = 20 * 60;

The background task for the sample app is trivial—it puts the thread to sleep for a second in a ...

Get iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.