nick shmick
nick shmick

Reputation: 925

how to detect when a certain time is passed when the app is in background?

I have a simple reminder and I use UILocalNotification to alert when the time I set in a date picker is now, and I want to change a certain label of a view controller when the time had passed...lets say a user selected a date, and after the reminder was reminding him something, I wand when he gets back to the app to see some label that wasent there before, how can I detect when a time passed when the app is in bg?

I have this method in my delegate:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"bammm! its time to get sh#@ done." message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}

but this only happens when the user come back to the app from either clicking on the popup or sliding the notification that you get when the iphone is locked.

thanks!

Upvotes: 1

Views: 202

Answers (2)

Duncan C
Duncan C

Reputation: 131398

When you enter the background, save some information to user defaults. (Like the future date after which you want to display a message)

On launch, and on returning from the background, load that data from user defaults and check to see if it's time to install the message. If it is, display it.

Upvotes: 0

jeffro37
jeffro37

Reputation: 706

There isn't any hook that gets hit if the notification fires while backgrounded and the user doesn't launch the app from notification.

In this case, it might make sense to manage the label with an NSTimer, to be set at the same time you schedule the notification. When the user backgrounds the app, you'd invalidate that timer.

When the app is brought back to the foreground, you'd check the current time against the target time of your notification, and update your label if that time has been hit. If that time is in the future, you'd set a new NSTimer based on the target time.

Upvotes: 1

Related Questions