I have done research how to maintain the timer running when home button is pressed. But i am quite confused.
This is my code, how can i fix it and the timer continue running in background? Thanks in advance.
-(id)init {
if (self = [super init]) {
self.timer = [[NSTimer alloc] init];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
}
return self;
}
+(Timer *)sharedSingleton {
static Timer *sharedSingleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSingleton = [[Timer alloc] init];
});
return sharedSingleton;
}
-(void)startTimer {
i++;
_count = [NSNumber numberWithInteger:i];
[[NSNotificationCenter defaultCenter] postNotificationName:COUNT object:_count];
}