iOS7 introduces the background Fetch API which allows an app to get updated content when the app isn't running. iOS intelligently schedules the background fetch events based on your app usage so when you open your app the content is always up to date. In this app we will simulate background fetching to get the current time into our app.
Open Xcode and create a new Single View Application. For product name, use iOS7BackgroundFetchTutorial and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices.
To enable the abiltiy of background fetching for our application we need to go to the project settings and select Capabilties. Set the Background Modes switch to On and check the Background fetch option.
Next, go to the Storyboard and drag a Label to the main view.
Select the Assistant Editor and open the ViewController.m file. Ctrl and drag from the label to the @interface section and create the following outlet.
Add the following line to the end of viewDidLoad
self.timeLabel.text = @"12:00:00";
When we start our app initially the timelabel wil have a time stamp of 12 o'clock. Next we will add the alterTime method to save the current time to our label.
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *timeString = [dateFormatter stringFromDate:now];
self.timeLabel.text = timeString;
This method will be called when we simulate the background fetch later on. Go to the appDelegate.m file and import our ViewController header file.
Next, change the to application:didFinishLaunchingWithOptions method to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Set the fetch interval so that it is actually called
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
The recommended value for minimumBackgroundFetchInterval is UIApplicationBackgroundFetchIntervalMinimum, which means iOS itself will figure out how often it needs to fetch data on the background. When a background fetch occurs, iOS starts the app and then makes a call to the application delegate method application:performFetchWithCompletionHandler.
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
NSLog(@"Current Time Altered");
// get current view controller
ViewController *mainViewController = (ViewController *)self.window.rootViewController;
First we create the mainViewController variable to get a reference to our current view controller, and then we call the previously created alterTime method. Build and Run the project and the application is started. Press the home button of the iOS Simulator and in Xcode select the Simulate Background Fetch option in the Debug Menu. Go back to the iOS Simulator and click the application icon on the springboard. You will see that the current time is displayed.
You can download the source code of the iOS7BackgroundFetchTutorial at the ioscreator repository on github.