0

Is there anyway to keep a timer running after the application is terminated (not in background). I want to do this so a local notification is triggered once the timer reaches 0.

Can UserDefaults be used to achieve this?

  • 1
    what kind of app is this? iOS or Macintosh? In general, NSTimers are killed off when the app quits. – Michael Dautermann Sep 21 '13 at 06:27
  • 1
    have you tried using [localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]]; –  Sep 21 '13 at 06:29
  • 1
    possible duplicate of [NSTimer Keep counting after app is closed in iPhone](http://stackoverflow.com/questions/10869835/nstimer-keep-counting-after-app-is-closed-in-iphone) – jscs Sep 22 '13 at 04:26
  • It is an IOS application. An no I haven't Ankit I will look into it – Jean-Claude Oct 01 '13 at 07:51

2 Answers2

1

Guessing you are talking about iOS, have a look at this: https://developer.apple.com/library/ios/documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html

NSTimer is not what you looking for, since it's only working at the runtime of your App. UILocalNotification let you create a notification (including scheduling it for s specific time) handled independently by the system.

sofacoder
  • 664
  • 6
  • 19
  • While redirecting to a link is appreciated, Please consider giving your point to view/solution to the issue. – thatzprem Sep 22 '13 at 04:27
1

NSTimer cannot run infinitely in background. NSTimer is paused when your application enters background.

You have to use repeatInterval property of UILocalNotification to achieve it.

UILocalNotification *alarm = [[UILocalNotification alloc] init];
alarm.fireDate = [NSDate date];
alarm.timeZone = [NSTimeZone localTimeZone];
alarm.repeatInterval = NSMonthCalendarUnit;
[app scheduleLocalNotification:alarm];
thatzprem
  • 4,697
  • 1
  • 33
  • 41
  • upvoting & accepting it as answer are equivalent ways of express gratitude :) - please consider do so if it did really help you out. – thatzprem Oct 01 '13 at 15:29