iOS background task - ios

I have a simple application connected to a server, that should download from it some data regularly. Basically, the server stores a number which increases over time; the user has a certain number and could ask to be notified as soon as the server gets close to his number.
Therefore, when the user requests to be notified, a service should start and get info (every approx 40 seconds) about the current number. When user and server numbers are close enough, a local notification should be displayed.
I had no problems in implementing this in Android (AlarmManager + Background Service), but I can't in iOS. I read that for long running background tasks in iOS my app should be a music app, a voip app, a newspaper app, etc.. but mine is none of those. All I can do is run a background task when the app enters background, but it lasts no more that 2 minutes before the app gets killed.

Related

iOS Location - Significant Location Change in iOS 8/9 can start standard location service?

Many questions concerning iOS Location tracking have been asked on here, but recently Apple has updated much of their functionality and so many of the answers are obsolete, my question is specifically targeting iOS 8 and 9.
I am interested in creating a location tracking application which will accurately track the user even when the app is closed. To do this, apple states:
If you leave the significant-change location service running and your
iOS app is subsequently suspended or terminated, the service
automatically wakes up your app when new location data arrives. At
wake-up time, the app is put into the background and you are given a
small amount of time (around 10 seconds) to manually restart location
services and process the location data.
So my understanding is, if you have all of the necesarry permission and plist keys present, the OS will wake up your app and allow you to do something for 10 seconds, or if it needs more time it can:
If an iOS app needs more time to process the location data, it can
request more background execution time using the
beginBackgroundTaskWithName:expirationHandler: method of the
UIApplication class.
Now, this says that if my app needs more time to process the location data, it may start a background task (with an expirationHandler) to handle that. When reading about background tasks, I read that background tasks can run for a maximum of 10 minutes
So my overall question is, when my app gets woken up when a significant location change has occurred, is it possible to start the standard location service and have it run in the background indefinitely? Is it possible to start another background task before the first one expires?
EDIT:
From the Apple Documentation it says:
because it wakes the system and your app at least every 15 minutes,
even if no location changes have occurred, and it runs continuously
until you stop it
So my idea is, if the user starts their phone from nothing and never opens the app, after at most 15 minutes (even if they are standing still), the OS will fire my app, in which case I could start a background process that I can start the GPS tracking in, and thus getting accurate GPS location. Would this logic work with the OS?
The main problem is that significant location changes cannot be used for
"accurately track" the user.
Significant means 1000m, since it is cell tower based, and not GPS based.
So your app is woke up when the user moves into the next GSM cell.
This is not acurate enough for acurate tracking.
finally this means you cannot acuratley track when the app is closed, this works only in running app or background running up.
"When reading about background tasks, I read that background tasks can
run for a maximum of 10 minutes"
No, thats not true. My app runs in background and records GPS locations until the battery is drained (8h+). (It does not use the significant location mode)

Waking up an app from background by a time interval?

is it possible on newest ios to make a tracking app wake up every x minutes in order to send the stored locations to an url, even if the user is not moving?
Requested functions of the app are:
App is getting locations based on distance filter (example: only get coordinate via gps, when user has moved more than 10 meters.
App is not allowed to send every new position immediately to url
App should store all new locations of the last 3 minutes
App should send stored positions (of the last 3 minutes) in an interval of 3 minutes. (! Even if user has not moved since a while, app shall connect to url.
App is mainly running in backround.
Example case:
User has moved more than 10 meters at 9:00:11 to position 53.432, 13.245, at 9:01:20 to position 53.235, 13.435 and at 9:03:10 to position 54.002, 13.768. No more movement of more than 10 meters before 9:15:00.
So from 9:00:00 until 9:15:00 device has located 3 coordinates.
Order from server is: Send all locations in an interval of 3 min., starting from 9:00:00.
Requested result is:
At 9:03:00 app sends all positions from 9:00:00 until 9:03:00:
9:00:11: 53.432, 13.245,
9:01:20: 53.235, 13.435
At 9:06:00 app sends all positions from 9:03:00 until 9:06:00:
9:03:10: 54.002, 13.768
At 9:09:00 app sends all positions from 9:06:00 until 9:09:00:
„no new positions“....
Problem is:
App has to be waken up, in order to process actions like sending informations to the server.
One event, that could wake up the app would be a movement of more than 10 meters.
But since user is not moving from 9:03:10 until 9:15:00, there is no event that wakes up the app. App stays suspended and will not send the location of 9:03:10 before next movement at 9:15:00
Solution?
Are there other possibilities to make the app wake up and send stored locations at 9:06:00?
Can apps have an internal timer to wake up?
(According to my understanding a timer app is not waking up, when the alert sound should be given. Instead it is scheduling a local notification to fire at the time of the alert.)
But if it is possible to schedule local notifications, can´t an app schedule other actions like „wake me up“?
Thanks a lot for your time and help!
the app can attempt to run background tasks, but it can't force itself to stay in existence - the OS will shut it down after a couple of minutes, regardless of local timer handlers or notifications.
Your best bet is that as soon as the user has moved more than 10 metres, the server starts sending push notifications. You can have a handler which can wake the app in the background, and you can use this handler to send data back to the server.
There are a lot of rules regarding when to use certain background modes, so in case you haven't come across it before, check out this link.
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

iOS GPS Tracking app shutdown in background

I'm currently working on an iOS app using objective-C that send every minute gps coordinates to my API.
I've used AFNetworking & CoreLocation, I also wanted my App to run all day long, but only send coordinates if time is between 8am - 6pm.
Everything's working fine on iOS simulator targetting iOS9 my app is sending his location every 1 minute to my API, even if i press home or lock the phone.
PhoneCall, loss of network or GPS have been handled in order to keep my task running.
But when I tested it on some device (iPhone 4S on iOS9) Location stop updating after an average time of 1hour to 1hour and a half.
Is iOS shutting down my app in background after a given time?
If so, is there a way to keep my app doing her job in background for at least 10 hour, without having to prompt the device user?
Implementing long running background task are "allowed" by apple if it concern:
"The app keeps users informed of their location, even while it is running in the background."
Does that mean background task will block my http request after a while?
For long term deployment, I wish my app to be upload on the App Store, but I've read many post about app GPS tracking app which have been rejected.
Here's the minimal list of requirement my app should have:
Send GPS coordinates to my API every minute
Working in background for a minimum time of 10 hours without shutting down or prompting user to reload the view
Could those requirement be accepted to get my app upload on AppStore?
Or will I have to use local/remote notification and prompt the user to reload my app in order to keep it running for such a long time?
The documentation says.
The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.
I think this will be apt for your scenario.

How to tackle an app that fires recurring local notifications but on a changing schedule?

I'm creating an app that needs to fire local notifications for sunrise and sunset on a daily basis.
Sunrise and sunset times changes every day.
I want the registering of the local notifications to take place even if the user didn't open the app (i.e. a background task). So the user installs the app, sets his location (since sunrise and susnet calculations are location based) and then he knows he'll get notifications for sunrise and set every day without needing to open the app or being connected to the internet.
Ideally I need a background task for registering local notifications over a given period which runs at least once every period. "e.g. registers local notifications for the next week, which in that case I need it to run at least once a week".
None of the current background modes fit my case, I tried a background fetch, but since there're no data downloaded iOS stopped running my background task after a couple of days
I know I can use push notifications, but I don't want to unless it's my last option, cause I want the notification to take place even if the phone was not connected to the internet.
So how do you think I can tackle this? Is there something I'm missing?
Get Location for every ’n’ minutes in background. By Using beginBackgroundTaskWithExpirationHandler and NSTimer .it keeps app live in background.
So, You can schedule a localnotification in background.
For getting location update in background Refer the link
How do I get a background location update every n minutes in my iOS application?
(From iOS7 finite-length background task time reduced to 180 seconds. so you must fetch location within next 180 seconds.)
If user force quit the app. the app destroyed all the task . Use Significant location change or region monitoring to restart the Background task and get location for every ’n’ minutes.

ping server when the app is in background

I need to keep the app to send a simple GET request ("ping") to the server every minute just for it to know that user is still online. I have no problem with it while the app is active and 10 minutes after it went background using "beginBackgroundTaskWithExpirationHandler".
But how can I keep request repeated all the time unless user terminates app manually?
That's not really how it works -- it's not a Windows or a Mac app that runs continually in the background.
What you probably want to do is use the background fetch process, whereby iOS will periodically run a particular method allowing you to do network operations while your app is in the background (or not running at all).
What this won't allow you to do is ping your server exactly once every minute. But actually that's a good thing. Consuming a users battery and data allowance like that is not very user-friendly.

Resources