Is it possible to "suspend" the cllocationmanager while in the background?
I'm trying to stop the manager then restart it at a specific time all while in the background. Is this possible? I tried stopping the manager and starting a nstimer, but it doesn't work because nstimer does not run while in the background.
My objective is to have the manager run during certain time intervals, "suspend" during certain time intervals, and start after the suspend time is up in order to preserve battery life.
Thanks for the help!
The short answer is: No.
The long answer is: Apple has thought of that and has a solution for you.
You should read about App States and Multitasking and look for "Tracking the User’s Location" subtitle.
Related
I have a timer app, it counts up for an indefinite amount of time (until the user stops the timer). It works much like the default iOS clock stopwatch.
I have read the articles about being a responsible background app. I don't have any UI changes happening and stop the timer when the app goes in the background and resume the timer upon returning to foreground. The app does send a local notification every 30 minutes or so to remind the user to take a break.
Problem: Without fail my app is terminated after a few hours (between 2-6 or so hours) and the current timer and info is lost.
Things I've tried:
-As aforementioned I changed my app to follow the guidelines of being a responsible background app (before I had the timer UI counting up and also the timer running, even while in background). I thought this would solve the problem since my app isn't using up much memory or doing any intensive tasks.
-I also have tried saving out the data of the current timer (to NSUserDefaults) in ApplicationWillTerminate and then restore it on DidFinishLaunchingWithOptions. It doesn't seem to work. It's almost like my app is force quit or something because if ApplicationWillTerminate was being called then it would save the data and restore it upon the app launching again. Mystery.
-Currently I'm saving an NSDate of the start time of the NSTimer on ApplicationDidEnterBackground, invalidating the timer, and starting the timer and restoring the start time upon ApplicationWillEnterForeground. Still no luck.
The thing is this is affecting a very small percentage of my users--so it's not like it's a problem for everyone, which makes it more confusing. I still want to fix it of course because it's a pretty lame bug--and I've encountered it myself (on an iPhone 5), so it's not necessarily an old iPhone low memory somethingorother problem.
This has really boggled me, I've done my research and also scoured fairly well the stack overflow questions and don't seem to find much of anything to help me.
Any help would be appreciated, feel free to ask more questions or tell me to clarify.
applicationWillTerminate is basically never called. It will not be called when your app is in the background and it gets removed from memory. You should expect your app to be removed from memory if it hasn't been used for a while. How long "a while" is depends on what else the device is doing at the time... Best to use applicationDidEnterBackground: or applicationWillResignActive:.
When your app goes into the background, record your timer's elapsed time (eg: in NSUserDefaults or however you prefer). Also record the current time. When the app comes back into the foreground, look at the two values you recorded and update the elapsed time accordingly, ie:
elapsedTime = savedElapsedTime + (currentTime - timeWentIntoBackground)
There's really no reason your app needs to remain active in the background burning the user's battery just to keep track of seconds ticking by.
Also, don't rely on applicationWillTerminate. Use applicationWillEnterBackground.
I m developing an app in which I have to track the LocationUpdate in the Background as well as in the Foreground but after some time interval in the Background the app stops updating the Location. When I take it back to Foreground it starts again and when I put it in background after some time interval it stops again.
I am not able to find the issue please help me if possible.
You have to specify that your app need location updates when in background in plist
Here is apple docs Getting Location Events in the Background
and here is a tutorial for this.
And I would recommend you to read Location Awareness Programming Guide
disable distance filter
set desired Accuracy to best
use - (void)startUpdatingLocation
Now in ios6: there is
locationManager:didUpdateLocations:
in ios5 there is
locationManager:didUpdateToLocation:fromLocation:
the first delivers the locations buffered in the background and keeps your application sleeping and wakes it up with a sequence of fixes, while the ios5 method delivers the location all of the time.
I am doing the iOS timer application, but when I press the home button the timer will stop.
I would like to know if close the application, the program can keep working?
And go back the application can keep running.
I see some article How to write a background service in iphone application? said can't run in background.
Can some one help me?
Thank you.
If it's a timer you are doing, you can always keep the time when it started, through CACurrentMediaTime(), and if your application goes in the background, when it comes to the foreground again, in applicationDidBecomeActive:(UIApplication *)application, take another CACurrentMediaTime() poll, and display the difference between the numbers.
I believe this is how the Apple's bundled app works. No need to keep processing that number if the application is not used.
I have gotten a NSTimer to fire a selector every second correctly. However, I require it to continue firing even when it goes into the background. I have even tried the jailbreak tweak called Insomnia, which supposedly stops apps from sleeping in the background, but to no avail. So what would be the best way to achieve my objective?
Any help is very much appreciated!
There are only certain things you can do as background tasks in iPhone apps. The Apple documentation explains how it works and what you can do. The apps that use some of the background features don't continuously run - they are woken at 'appropriate times'.
I have a application that periodically (via an NSTimer) asks for the users location using locationManager:startUpdatingLocation I want the locationManager to run in the background so have entered "UIBackgroundModes = location" in the info.plist file.
My question is I am not seeing the results I expected, am I right in thinking that when I press the home button the application delegate calls "applicationDidEnterBackground" but although locationManager is allowed to continue my NSTimer is getting suspended (and as a consequence its not calling startUpdatingLocation to periodically query the devices position). Any ideas / solutions would be much appreciated ...
Gary.
All NSTimers are invalidated when entering the background. You need check via the CLLocationManager for any changes.
Just keep the startUpdateLocation running, you delegate will receive any major changes of the location.
When running in the background, you will only receive location changes based on cell towers.
If you read the documentation for -applicationDidEnterBackground: you'll find that yes, your timers are invalidated. Furthermore, the iOS Application Programming Guide tells you exactly how to track location from the background.