When iPhone app is in background state or when app is minimized, I want to monitor the duration of time the app is in background and on every regular interval like every 24hrs need to get updates from web server and write the modifications in my local database.
This monitor cycle will repeat as long as app is in background. Is this kind of task is possible in iPhone applications ? Could any body help me how can I perform these tasks when app is in background state ?
beginBackgroundTaskWithExpirationHandler: is the API to make the app in background for long tasks but how long will this support.
Background tasks are killed after 10 minutes time, so to answer your question: No, it's not without faking some kind of long term event handling through a significant location's update, and even then, you have no control over when exactly the events are sent. On the other hand, using APNS to alert the user to a change has the possibility of being ignored, but comes with regularity.
Related
I am trying to call an endpoint after every 5 minutes and it's working fine with Timer when app is active but when app goes to background the Timer stops firing because timers don't work in background. Is there any possible way to achieve this even with the use of private api because i am not planning to upload the app on AppStore.
In a word, no. Apple goes to significant lengths to prevent apps from doing this sort of thing since it is a significant battery drain.
You might look at using the BGTaskScheduler API to schedule background requests that you would like the system to perform on your behalf, but you won't get a frequency anywhere near every 5 minutes.
As far as I know there are 3 states an app can be in.
a) Foreground
b) Background
c) Sleep (which is a state of the whole device)
The difference between the first two seems simple. But what exactly is the third state? And when does it happen? Is it when the device is locked?
And, quite importantly, does this happen after a long time, or does the device go to sleep if the user doesn't use it for a second, and reawakes the moment there's user action?
I've read different and seemingly contradictory pages about it, so either they were referring to different states or I just misunderstood what they were saying.
What am I trying to achieve: several things. But one of them is a timer that will execute an action every 10 seconds, and if the device was asleep so timers didn't run - I want it to run at the earliest possible time. Should I set a timer for every second and check if 10 seconds have passed (so that if a user checks the time on the phone for one second, the action will happen) or will it work automatically even if I set the timer for every 10 seconds?
The application lifecycle and the potential states your app can be in are more complex than you have stated.
Apple has comprehensive documentation on the application lifecycle and includes this diagram:
The main states you need to be aware of are:
Foreground Active - your app is actively executing in the foreground (ie it is the onscreen app)
Background - your app is actively executing in the background
Suspended - your app is in memory, but not currently executing.
It is important to note that your app only executes in the background for limited durations and under specific conditions.
It will execute in the background briefly as you transition from active to suspended in order to give you a chance to perform any required housekeeping prior to being suspended
It will execute in the background in response to specific background execution modes and permissions, such as location updates, Bluetooth or streaming audio.
When many people talk of their app "being in the background", what they really mean is that it is suspended. The Background state is an executing state while the Suspended state is a non-executing state. While suspended your app is still held in memory and will resume executing where it left off when it resumes an execution state.
Timers do not fire when your app is suspended. That is, expiration or firing of a timer will not bring your app from the suspended to background execution state.
Your best strategy for tracking missed events is to record when an event should fire (ie the fire Date) and check if that is the past when your app enters the Foreground Active state.
When your app is the foreground app and the device screen locks, timers will stop "ticking", so your strategy of setting a one second timer is probably the best approach for that scenario.
E.g. You have a 10 second timer, with 5 seconds elapsed when the device sleeps. After waking the device a further 5 seconds will elapse before the timer fires.
Note, however, there is a good chance that when the user does wake the device they will open some other app your app will be suspended.
You should also be aware that your app can be jettisoned (removed from memory) at any time when it is in the Suspended state if iOS needs memory. In this case when the user re-enters your app, it will restart from the top-left Unattached state and any in-memory variables will be lost.
Our app using location tracking in background mode but after minimize the "applicationwillterminate:" method called after 20 to 50 minutes. Its happened only sometimes not every time.
Use background task with a expiration handler. Check for backgroundTimeRemaining and call endBackgroundTask(_:) when you get location data.
If you do not end your tasks in a timely manner, the system terminates your app.
Use background task as described in Updating Your App with Background App Refresh, to update the data opportunistically.
I'm writing a iOS/Swift application which reads data from a REST service each X minutes and updates the UI accordingly.
Now I would like that when the app is put in the background, a task keeps being invoked at X minutes intervals reading from the REST service and, in case the data just read satisfies a given condition, show a notification prompting the user to bring the app back to the foreground.
In my searches I've read that during applicationDidEnterBackground event, I should start a task with beginBackgroundTaskWithExpirationHandler.
The problem is that, if I've understood correctly, this allows a maximum of 10/15 minutes after which the app is terminated if the task is not stopped with endBackgroundUpdateTask, while I want the task to keep polling the service indefinitely (at least until the user disable it from the app's settings)
My question is:
How is this kind of functionality performed normally? Do some common solutions or best practices exist for the solution of such a problem?
Use iOS Background Fetch feature where you can specify minimum background fetch interval. But actual interval between successive invocation of your code will be determined by iOS framework. For details checkout this link: http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520
I use this approach in my app and I think it is a preferred way of doing.
You can use a local notification that can be presented from the background in case your condition is met.
Correct, iOS will eventually shut down the background process, you can't enforce continuous background activity. Use the backgroundTimeRemaining property to check how much time your application has left and try to handle it as gracefully as possible by calling endBackgroundTask so that iOS does not force kill your app.
As a solution, you could think about using remote notifications with with content-available : YES, which runs the didReceiveRemoteNotification
Have a look at the Parse.com Their local datastore is an abstraction for what you are trying to acheive.
By the way, is it really necessary to refresh in the background. If call is relatively quick, there is no need to refresh until the user open's the app. Background processes like that, using the net can be quite battery consuming when the user are not on a Wifi. So consider the use case carefully!
I'm writing an app for a client where the users will enter data during the day, then at a given point at night (say, at midnight) the user's data for that day should upload to the server.
Therefore I need to schedule a method to run on my app at midnight, even if the app is in the background. This method will then sync the collected data with the server and download any changes.
In order to achieve this, I imagine I need to set the app to always run in the background (i.e. longer than 10 minutes) then schedule the function to run after a specific time. Do I do this by using performSelector: withDelay:? Or do I need something more robust because the app will be in the background?
Thanks guys!
You might check out UILocalNotifications. You could schedule that event to happen and then set a badge icon to let the user know it processed something (or not).
Info on these HERE
I don't think your app or what you are trying to do is qualified for Apple's requirement for long running background task. Check out the "Implementing long-running background tasks" section of this doc from Apple.