Reputation: 12595
How can I have an NSTimer going throughout the entire time the user is in the app?
I want to be able to change view controllers and perform different functions, all while a timer is going on int the background. If the timer hits 0, then I want to fire an event. Can I also have a timer going inside the app while the app is in the background? Or even when the iPhone's screen is off?
Thanks for the answers!
Upvotes: 0
Views: 164
Reputation: 976
Run this method in app delegate
-(void) timerfunc{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
timer = [NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(yourfunction) userInfo:nil repeats:YES ];
[pool release];
}
Upvotes: 1
Reputation: 1807
Do this piece of code in the Appdelegate making the timer its property.
self.timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer
target: self
selector: @selector(timerExpired:)
userInfo: nil
repeats: NO];
//Run the timer on the runloop to ensure that it works when app is in background
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode: NSDefaultRunLoopMode];
Upvotes: 2