I want to poll the location from GPS in the background. So we used a NSTimer and performed periodic checks in the timer tick. In other words, can we schedule NSTimer while the app is in Background?
Thanks in advance
You can't periodically execute arbitrary code, but you can get pinged on significant location changes via startMonitoringSignificantLocationChanges in CLLocationManager.
Related
I am creating a voip application for iPhone and iPad family in which I have done following things :
Added the UIBackgroundModes key in the plist file with values 'audio' and 'voip'.
Create NSInputStream and NSOutputStream with tag NSStreamNetworkServiceTypeVoIP and scheduled them in runloop of another thread(not main thread).
Created a background task in applicationDidEnterBackground.
Added setKeepAliveTimeout handler (timeout value 600 sec).
Application relaunches when code in handler of setKeepAliveTimeout is called.
Application relaunches if I dont put setKeepAliveTimeout handler ,but tries to send any signal to app after suspension(10 mins in background is completed).
I have tried almost everything that came in my mind, Need pointers towards the solution Thanks in advance,
It is the duty of the setKeepAliveTimeout handler to care about the connection. Thus, you should use an alive interval that is shorter than the timeout time of your connection.
I know how to set a timer and when timer expires, it fires and a action is caught.
What I'm expecting is that after 30 seconds of every one hour of real time that that "xx:00:30" I have to check my server for updates.
I know how to communicate with server. But how can I create such a timer that fires every "xx:00:30".
I don't want to run the timer if the app is in background.
Any ideas on how to do this?
As mentioned by Marcus in his last message,
NSTimer's initWithFireDate:interval:target:selector:userInfo:repeats: or setFireDate:
works.
This really leads off from my "brainstorm" last night here:
iPhone Brainstorm - CLLocation in Background - Polling every 15 minutes
When my application is active I can stop the CLLocationManager after I get a correct reading. Then after the performSelector afterDelay I can start it again, take a reading and stop it.
If though the application is but to the background when I have a timer running it never restart the CLLocationManager. I have registered for background location updates and if I do not stop the CLLocationManager it is fine but for battery I was hoping I could keep start/stopping it.
Please advise.
Thanks
James
Save the current time when the application enters the background.
When it becomes active again:
If > 15 minutes has passed since the application entered the background - start CLLocationServices
If < 15 minutes has passed, start at timer with the remaining time until CLLocationServices needs to start.
Read the Apple documentation on Location in background.
Timmers can't be used in the background.
If you keep CLLocationManager running with the correct settings it will only call location update on major change, which is when the user moves between cell towers/wi-fi points.
Is there a way to keep timers running when an App enters background mode without location services running?
If I have a timer running in the App with location services on and enter the background then it keeps running. However calling stopUpdatingLocation also stops the timer. I would like the timer to be able to call startUpdatingLocation at some point in the furture but as it not being run it can't do this.
Any information on how multi-tasking works with location services would be helpful.
You should probably figure out when (at what time) the timer would call startUpdatingLocation and add a UILocalNotification at that time.
I have created an app that uses NSTimer, which gets triggered each second.
My problem is that if the Iphone is in sleep mode i get a delay for 10 to 15
minutes before the event is triggered. I have stackoverflowed and googled this
and the reason for this seems to be that the phone stops listening for certain
events when in sleep mode.
Some people have solved this issue by playing a mute sound, not allowing the
phone to sleep.
What could be the reason for the delay?
The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
If I use the mute sound solution will it the pass the apple review?
Code:
timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:#selector(goAction)userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void)goAction {
// Here i check for some dates and then call the activateBeepAlarmView
}
Well since no one has answered my three questions I will have to answer them:
1. What could be the reason for the delay?
I will have to quote Ben S:
Once applicationWillResignActive gets called on your application you simply stop receiving events:
The delegate can implement this method to make adjustments when the application transitions from an active state to an inactive state. When an application is inactive, it is executing but is not dispatching incoming events. This occurs when an overlay window pops up or when the device is locked.
The point of sleep mode is to save energy. To do so, the device stops listening for events like the ones you're asking for. NSTimer events will still fire since they don't require expensive (battery-wise) hardware monitoring. Also, alarms are implemented using NSTimer, so they need to be able to function even when in sleep. Otherwise, people might not wake up and blame their iPhone.
2. The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
No, currently I haven't found another solution, please feel free to correct me if I'm wrong.
Check out this blog post how to do it.
3. If I use the mute sound solution will it the pass the apple review?
Yes
When the iPhone goes to sleep, so does your app and the runloop that runs the NSTimer.
You seem to think that an NSTimer is some sort of hardware based timer. It is not. It operates completely within the software of the app that launches it. I don't know what is waking your app up but it is definitely not the NSTimer.
In short, what you want to do is impossible. You can't sleep the phone and then have an app still active and running. The mute sound technique is just a kludge to keep the phone awake and the app running.
If you need the phone to stay awake, you need to set the application's idleTimerDisabled to YES. This will prevent the phone from sleeping and the app can remain active. But once you let the phone sleep, it cannot be awaken from app code. Only the hardware can do that in response to an alarm or an incoming message.
What happens in your app when the NSTimer is triggered each second? Please provide code showing the creation of the timer as well as the code for the selector that is called when the timer completes.
Also what do you mean by a "delay for 10 to 15 minutes"? Is the delay always that long or is that how long you wait to awaken the iPhone and then the event is triggered?
Depending on what you need to do every second you can handle this situation in different ways. Please respond and we'll try to work our way through this.
Bart
#Jakob,
This is impossible with the "Official SDK". If you're developing apps for jail broken phone, then you can use IOKit framework for this. For more info please refer this.