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?
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?