NSTimer expires when the device is locked - iphone

My application supports the GPS and it runs both in foreground and background. I have started a timer , which is working perfectly unless the device is locked.
I want the timer to keep on running even when the device is locked.
Can anyone tell me how this can be done ?

Whichever way you start a timer, the default run mode is like below:
Timer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(run) userInfo:nil repeats:YES];
[[ NSRunLoop currentRunLoop ] addTimer:timer forMode:NSDefaultRunLoopMode ];
using NSDefaultRunLoopMode. You want more flexibility, so change forMode to take NSRunLoopCommonModes instead and you should be fine.

Related

Whats the logic behind NSTimer when applicationDidEnterBackground [duplicate]

I don't understand it at all but NSTimer in my app definitely is running in background. I have a NSLog in method run by the timer and it is logging while it's in background. It's on iPhone 4 with iOS 4.2.1. I have declared location background support in Info.plist.
I read the docs and many discussions here and elsewhere and it shouldn't be possible. Is it an iOS bug? Or undocumented feature? I don't want to use it and find out in near future, for example with coming of iOS 4.3 that Apple silently "fixed" it and the app won't be working.
Does anybody know more about it?
NSTimer is going to fire whenever the main runloop is running. Apple makes no promises that I know of to unschedule timers or to prevent the main runloop from running. It's your responsibility to unschedule your timers and release resources when you move to the background. Apple isn't going to do it for you. They may, however, kill you for running when you are not supposed to or using too many seconds.
There are many holes in the system that will allow an app to run when it isn't authorized to. It would be very expensive for the OS to prevent this. But you cannot rely on it.
You can have a timer fire while in background execution mode. There are a couple of tricks:
You need to opt into background execution with beginBackgroundTaskWithExpirationHandler.
If you create the NSTimer on a background thread, you need to add it to the mainRunLoop manually.
- (void)viewDidLoad
{
// Avoid a retain cycle
__weak ViewController * weakSelf = self;
// Declare the start of a background task
// If you do not do this then the mainRunLoop will stop
// firing when the application enters the background
self.backgroundTaskIdentifier =
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundIdentifier];
}];
// Make sure you end the background task when you no longer need background execution:
// [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Since we are not on the main run loop this will NOT work:
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:#selector(timerDidFire:)
userInfo:nil
repeats:YES];
// This is because the |scheduledTimerWithTimeInterval| uses
// [NSRunLoop currentRunLoop] which will return a new background run loop
// which will not be currently running.
// Instead do this:
NSTimer * timer =
[NSTimer timerWithTimeInterval:0.5
target:weakSelf
selector:#selector(timerDidFire:)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer
forMode:NSDefaultRunLoopMode];
// or use |NSRunLoopCommonModes| if you want the timer to fire while scrolling
});
}
- (void) timerDidFire:(NSTimer *)timer
{
// This method might be called when the application is in the background.
// Ensure you do not do anything that will trigger the GPU (e.g. animations)
// See: http://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW47
NSLog(#"Timer did fire");
}
Notes
Apps only get ~ 10 mins of background execution - after this the timer will stop firing.
As of iOS 7 when the device is locked it will suspend the foreground app almost instantly. The timer will not fire after an iOS 7 app is locked.

updating screen every minute automatically on iPhone

I have a countdown TextField which I like to update every minute automatically in my iPhone app.
When the specific screen is visible it should count down - when switching to another screen the countdown obviously must not be updated.
When returning to the screen the countdown should continue again...
What would be the correct way of handling this?
Many thanks!
I would use an NSTimer. Here is the documentation for NSTimer.
Here is a snippet of code on how to set up a timer and get it to call a method:
NSTimer *theTimer = [NSTimer timerWithTimeInterval:60 target:self selector:#selector(updateTime) userInfo:nil repeats:NO];
This timer will call the updateTime method every time it fires. I have set it to NOT repeat, and instead, I would create a new timer each time I call the updateTime method. That way if you leave the UIViewController behind it will not keep firing the NSTimer.
Here is a generic updateTime method:
-(void) updateTime
{
//Update user interface
NSTimer *theTimer = [NSTimer timerWithTimeInterval:60 target:self selector:#selector(updateTime) userInfo:nil repeats:NO];
}
The timeInterval is in seconds, so this timer will keep firing every 60 seconds. You may wish to cut it a little shorter, like 30 seconds, or even 1 second and check the system time to see when you need to update your countdown.
I hope that helps!

MPMoviePlayerController notification question

Good Evening Fellas!
Is it possible for me to get a notification from an MPMoviePlayerController while it is playing a video. I mean, if i am able to get it every second, millisecond etc.
I have an indicator for the video current playback time that i want it updated continuously.
Thank you in advance.
The simplest way is going to be to set up a repeating timer yourself, and use that to update your display. e.g.:
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime:) userInfo:nil repeats:YES];

NSTimer only fires sometimes?

Im running into an issue where my timer is randomly not working. I can literally STOP the app and rebuild it without changing any code. It works sometimes, and sometimes does not?
When I say "sometimes" I am talking about per app session. If it starts up, it will remain running. But it is not running every app session.
I am seeing the same thing when using both code blocks below.
//Code1:
//I tried this block of code after reading it might be related to how I am interacting with my UIElements while the timer is running in the background.
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
syncWithServerTimer = [NSTimer timerWithTimeInterval:15 target:self selector:#selector(syncWithServer) userInfo:nil repeats:YES];
[runloop addTimer:syncWithServerTimer forMode:NSRunLoopCommonModes];
[runloop addTimer:syncWithServerTimer forMode:UITrackingRunLoopMode];
//Code2:
syncWithServerTimer = [[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:#selector(syncWithServer) userInfo:nil repeats:YES] retain];
Again, I tried both of these and they both work MOST of the time, but not ALL of the time.
What might be the issue? I am not prematurely releasing or invalidating.
So I guess its best to fire your timers on your Main Thread!
Seems to work everytime I do it now.
[self performSelectorOnMainThread:#selector(startTimer) withObject:nil waitUntilDone:NO];
the problem you are seeing (judging by your answer) is that the timer is destroyed with the run loop. if you schedule/add a timer on the run loop, the run loop will not run until all timers are exhausted.
if your timer should have the lifetime of the main thread or you cannot guarantee that the run loop will still be around, then add it to the main thread (although strictly, that is also not a guarantee that the timer will fire since the main run loop may exit).

How to make a function wait 30 seconds before it starts?

sorry still a newbie in the programming. Basically I have an Uibutton, when that button is pressed it would switch to another view, when it switches to that view, a function which calls a telephone number and sends an SMS text message out starts. What I'm trying to do it make that function wait 30seconds before it starts.
Any idea how to do this please?
just use:
-performSelector:withObject:afterDelay:
YOu can use NSTimer for that.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(myTimerMethod:) userInfo:nil repeats:NO];