Remind Users to Restart Your App

This tip shows you how to create a local notification that will be trigged when a user exits your application. I would recommend you use this sparingly, for example, if the user was in the middle of setting up configuration information needed by an application.

Let’s start with a quick look at how this appears to the user. Assume the application TestApp is running in the foreground. When the application is put into the background by pressing the home button, within 1 second (see the code below), the following local notifications will appear (iOS 5 and iOS 4):

If the user taps on the message, the application return to the foreground.

The code is quite simple, it begins with creating an NSDate object with the current date/time, adding one second to that time, and setting the fire date for a local notification to the configured date. From there, setup the local notification messaging and wrap up by scheduling the notification with the OS.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
 
  // Current date
  NSDate *date = [NSDate date]; 
 
  // Add one second to the current time
  NSDate *dateToFire = [date dateByAddingTimeInterval:1];
 
  // Set the fire date/time
  [localNotification setFireDate:dateToFire];
  [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];    
 
  // Setup alert notification
  [localNotification setAlertBody:@"Tap to return to TestApp" ];
  [localNotification setAlertAction:@"Open TestApp"];
  [localNotification setHasAction:YES];
 
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Once the notification has been presented to the user, and the user acknowledges, the method below in your application will be called once the app comes to the foreground:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  // Your code here...
}

Again, consider this option as a reminder to the user only in cases where an important process was underway, not as the default operation when the app goes into the background.

Comments are closed.