NSTimer stops in Background while power cable is disconnected - iphone

I have created GPS base Application.
In which App logged GPS data every 1 second.
App used the NSTimer to fetch GPS data every second.
This NSTimer is start in background. The NSTimer is start when app received the silent push notification from APNS.
I have seen a problem in iOS 7 that when Phone attached with power cable at that time timer call appropriately but without attached power cable timer stops while App in background.
Any inputs to resolve this issue greatly appreciated.

An NSTimer is not guaranteed to fire if your app is not in foreground. Once you unplug the cable the system puts your app into background to save battery.
Using an NSTimer is not the supported method to get location data. Your CLLocationManager will tell its delegate when there is a new location. No need to poll it.
If you need to track geolocation in background you need to declare location updates as a background mode from the capabilities tab in Xcode 5 target settings. Otherwise your location manager will stop delivering location updates once your app is not in foreground.

There are only few use cases that you can implement for foreground. If you don't want your application to be refused within review please don't use any hacks. Of course, you can use NSTimer in background, but it must be created in thread (runloop) of background task. But this background task lives for the only certain amount of time, so your timer must fire during this period. In other words, your first goal is to create background task, and only then you can use the timer. There are few cases suitable for your purpose (that allow you to create this task): 1.Monitor significant location changes or region 2.Fetch data (iOs 7). So please refer
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"BG fetch in action");
//the only 30 sec is allowed to perform all your actions
//during this period you can each second track GPS data
[self performSelector:#selector(finishBackgroundFetch:) withObject:completionHandler afterDelay:27];
[[NSNotificationCenter defaultCenter] postNotificationName:#"StartMyGPSRoutine" object:nil];
}
-(void)finishBackgroundFetch:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"BG fetch completion handler");
[[NSNotificationCenter defaultCenter] postNotificationName:#"ForceStopAnyBackgroundTaskCreatedWithStartMyGPSRoutine" object:nil];
completionHandler(UIBackgroundFetchResultNewData);
}
of your application delegate -- this will be useful in your case as well as monitor significant location changes.
You ought to put anywhere:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(setMinimumBackgroundFetchInterval:)]) {
NSLog(#"Set Force BG interval to %ld", interval);
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:interval];
}
In my case I fetch the GPS data each 5 min so 27sec to complete this task is not so long. Anyway you can play with time intervals with XCode. Please refer Main Menu->Debug->Simulate Background Fetch

Related

Stop setKeepAliveTimeout handler from being called after application came foreground?

For VOIP application we are sending PING packet to server every 10 minutes using setKeepAliveTimeout, everything works fine, however I'm not sure how to stop the handler from being called once application came foreground.
eg: Here is how I set the timeout
[[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
Background handler:
- (void)backgroundHandler
{
printf("10 minute time elapsed\n");
// do some action...
}
Above function is being called even after application came foreground, I have read in Apple documentation to set the handler nil to stop it. I have tried like below in applicationWillEnterForeground
[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:nil];
Still I'm getting call every 10 mins. How to handle this, Do I need to use flag only.
Any help is really appreciated.
you can do it like this
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
[[UIApplication sharedApplication] clearKeepAliveTimeout];
}
You have to invoke clearKeepAliveTimeout to stop the timer. setKeepAliveTimeout: is designed to keep a voip connection on and that's why it's periodically called.

background task running on iphone

I have some test code which i am using to keep my app to stay running in the background state, which works quite well on the iOS 5.1 simulator but is not behaving that way on the actual device.
Now i already know the requirements i have to follow,
so i have set "background modes" to voip and location.
In my delegate's applicationDidEnterBackground method i am calling the following function
which i call "doBackgroundActivity( )"
to request time for application to complete some long running task in background :
-(void) threadedMethod{
while(true){
NSLog(#"looping");
[self showLocalNotification:#"This notification will come every 2 min. if the app is running in background. Close it!"];
[NSThread sleepForTimeInterval: (5)]; // 60 sec = 1 min
}
}
-(void) doBackgroundActivity{
self->_backgroundTask = [ [UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask: self->_backgroundTask];
self->_backgroundTask = UIBackgroundTaskInvalid;
}
];
[self threadedMethod];
[[UIApplication sharedApplication] endBackgroundTask:self->_backgroundTask];
self->_backgroundTask = UIBackgroundTaskInvalid;
}
As you see, the doBackgroundAcitivity() just calls the threadedMethod() and all that does is run an infinite loop which sends a local notification every 5 seconds or so.
Now in the simulator when i run this, and minimize the app, i see a notification every 5 seconds or so. The application keeps on running in the background even when i run other applications i see my app sending notifications.
But this is not happening on the device. It looks like that the OS kills the app just after the first notification is send and i dont see any more subsequent notifications which i expect to see later?
Is there something else i have to do to keep the application running in background ?
If all you are looking to do is present a notification to the user while your app is closed you should use UILocalNotifications.
You can set when the notification should fire, as well as whether or not it should repeat, and the amount of time between repeats.
As a side note, i wouldn't recommend using sleep() in a loop to control when code is executed like that because it stops the thread from doing anything at all, and is bad for any other tasks that would like to use that thread. Instead you should use NSTimers, and other ways of controlling the time at which code is executed.

Run a background thread when application enter background

I need to schedule a task in background when the application enter background state.
I have to do this in order to call a remote service each x time and then show a local notification when some event happend with the remote service.
(I know it's looks like RPN, yes it is, but for some reason I am not able to use PRM)
I tried this code :
- (void)applicationDidEnterBackground:(UIApplication *)application{
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void){
remoteServiceCallThread = [[NSThread alloc] initWithTarget:self selector:#selector(doRemoteCall:) object:nil];
[remoteServiceCallThread start];
}];
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
[remoteServiceCallThread cancel];
}
I put breakpoint in the doRemoteCall selector, put is not working.
Maybe my approach is not the best one. If you have any other hack to doing this operation like I describe it I'll take it.
Thank you.
You are not starting the thread, it's initialization code is at the expiration handler block which will be called right before the app is shut down with a timeout:
A handler to be called shortly before the application’s remaining
background time reaches 0. You should use this handler to clean up and
mark the end of the background task. Failure to end the task
explicitly will result in the termination of the application. The
handler is called synchronously on the main thread, thus blocking the
application’s suspension momentarily while the application is
notified.
The task should be active for 10 minutes only (that is driven by iOS) if your app is not supporting one of the background modes (gps, audio, voip).
You also need to keep the returned UIBackgroundTaskIdentifier reference to be able to mark it as ended if the user brings the app to foreground or when task time is going to the end (that's when the handler block is called).

iPhone iOS do apps running in backgrounds generate and process notifications?

My app is downloading JSON objects in when the app enters background mode. The app converts them to core data entities. The issue that I'm running in is that I need to merge the managed object contexts for these core data entities with the main managed object context.
The way to merge these changes is through notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(contextHasChanged:) name:NSManagedObjectContextDidSaveNotification object:nil];
- (void)contextHasChanged:(NSNotification*)notification
{
NSLog(#"received notification with object: %#",[[notification object] description]);
if ([notification object] == [AppUser managedObjectContext]) return;
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:#selector(contextHasChanged:) withObject:notification waitUntilDone:YES];
return;
}
[[AppUser managedObjectContext] mergeChangesFromContextDidSaveNotification:notification];
}
For some reason my code does not receive these notifications while running in background. Does the app continue to generate notifications while it is running in background mode? Or is it something with the location of where/when I register for such notifications that's wrong?
Thank you for the clarification!
app continues to send notifications either in main or background. you need to take care of
the observer should not be released during add observer and posting notification. i thnk there may some mistak in implementaion read this
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html
Once you press the Home button, your app goes into suspended mode. It won't process the above notifications until you "wake" it up by tapping on its icon.
To ensure that your app continues to do its task, you need to request background task completion. The OS will then give you up to 600 seconds (10 minutes) to complete whatever task your app is doing before suspending it.
You can read more about it here: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW3
Specifically, look for "Background Execution and Multitasking" in the above link.
There are limitations to what type of notifications you can receive while in the background. Also the sending of notifications is something you schedule before entering the background.
If you need to continue doing work when the app enters thebackground you should call beginBackgroundTaskWithExpirationHandler as well.
Main documentation is here:
http://developer.apple.com/library/ios/ipad/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Scroll down to the section in Background Execution and Multitasking

Is it possible to know when the iPhone will go to sleep?

Is there any notification I can listen to that will alert me that the phone will go to sleep? I have implemented the following:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(resignActive:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
but it seems that it's not getting triggered when the phone goes to sleep after some time of inactivity (1 min in my settings). The reason I think it's not working is that I have a timer that perform an action every second and if a certain time has passed (bigger than one minute) it issues an audio alarm. In my resignActive I invalidate my timer and that works fine when I press the home button, but not when the phone goes to sleep. It still seems to be running in the background but "at a lower speed" as the times are much longer than normal (around 10 min instead of 2 min).
Any ideas what's happening when the phone goes to sleep? I have read these two posts but it doesn't really answer my question.
What happens to an iPhone app when iPhone goes into stand-by mode?
Iphone app is delayed for 10 -15 minutes when iphone is in sleep mode
Implements those 2 methods on the UIApplicationDelegate :
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
It will be called also when a push notification from another app (or a SMS) is shown...