Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
hey all. i want to develop an application which works like an alarm logic. i trigger the method which calculates the time with a timer but the problem is when i take the application to background the timer doesnt work. here my code is
Code:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundNotification" object:self];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ForegroundNotification" object:self];
}

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkTheIftarTime) userInfo:nil repeats:YES];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(theAppInBackground) name:@"BackgroundNotification" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(theAppIsActive) name:@"ForegroundNotification" object:nil];
}

- (void)theAppInBackground
{
    [myTimer invalidate];
    myTimer=nil;
}

- (void)theAppIsActive
{
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkTheTime) userInfo:nil repeats:YES];
}

how i can keep calculating the time when the application is in background?
 
hey all. i want to develop an application which works like an alarm logic. i trigger the method which calculates the time with a timer but the problem is when i take the application to background the timer doesnt work. here my code is

how i can keep calculating the time when the application is in background?

You didn't post code to the "checkTheIftarTime" method.

If you want your timer to persist, even if your app crashes or exits, then I would store the start time somewhere, preferrably in NSUserDefaults.
 
Timers don't run in the background unless your code creates a background task (UIBackgroundTask). Background tasks only run for about ten minutes. There are a few types of apps that can get around these restrictions but apple is strict about enforcing those restrictions.

Look at local notifications or even remote notifications if you want your app to be called back at a time that is more than ten minutes in the future.
 
thanks for the helps. i used local notifications as phone developer suggested
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.