iOS: communicating GPS position change to remote web service when app is running in background - ios

I would like to implement a geofence based domotic activation system.
There are different ways to define a geofence (e.g.). What I am concerned about is how this will all come together (service activation).
I am thinking of the use case where the geofence is set up by the user via phone (say via the App I am developing). Once this is done then the data will be stored in a remote webservice. iOS app development guidelines says that you cannot have an app running in the background and here my doubts start..
The point of defining the geofence is to activate some domestic/domotic service once the user enters the geofence that he defined as house. However if the App is running on background how can I get the app to communicate the position to the web service?
I have found this tutorial on remote background data fetching.
Apple developer documentation mentions that you can get background location change updates and hence I assume I can have some code to perform certain actions within the app when I receive a location change call back (from the location manager service). If this is correct I am then wondering whether combining this step plus using remote posting of data may be a valid solution (this has to happen when the app is in background mode).
If not would you be able to suggest an alternative approach?

I believe that if you request notifications within a geo fence you app will be woken up to get the notification, you can use this notification to then request a background session, which can then be used to update your web service.

Related

What is my best bet on periodically running a task to update Live Activity when my app is in the background?

I have an idea for an app that utilises Live Activities, inspired by the Flightly app (it starts a Live Activity for a plane ride): Basically I wanna use open data from an HTTP endpoint that is reachable via the onboard WiFi on a bus. E.g. you board the bus, connect to the onboard WiFi, open the app, it fetches details about the stops and then you start a Live Activity guiding you to a selected stop. So far so good. But sometimes the bus faces delays, etc. In that case, I need to update the Live Activity. That is easy when my app is running. It is not so easy, when my app is not running (or running in the background).
What is my best approach to regularly fetching data from the endpoint and updating my Live Activity? The problem is, that the endpoint is only reachable on the buses WiFi. So it looks like I cannot use APNS to push updates to the Live Activity (since my service running outside the bus cannot access the endpoint that provides the details and thus cannot push any meaningful updates). Background Tasks (e.g. background app refresh) run infrequently.
What options are left? I thought about receiving location updates in the background and acting on them to update my app state (and possible the Live Activity, if needed), although I haven't investigated that path in detail, yet.
Has anybody faced a similar issue and found a solution or can provide some guidance on how to approach this problem?
I think the easiest option is core location. So keep your application running by monitoring the position and then you can update the Live Activity by fetching the information from your access point. It should be fine for app review since you could use the the location to show a position on a map and only while the person is on the bus.
If your application isn't running you have to send the ActivityAttributes via APNS. Since the data cannot be accessed from a remote server that would communicate with APNS there is no direct way to use ActivityKit push notifications. An indirect approach (which I would not recommend) would be to send silent push notifications triggers every 5min or so and fetch the newest information when the application is woken up. But this won't work when the app is force-quit and using silent pushes to trigger polling will give you a bad score by the system and eventually the frequency will be throttled from APNS.

IOS/SWIFT use gps ,internet and calculation on the background

As a new iOS developer I am need to ask if it possible to run a background service on the phone when the application close and in this service to use user location, send the location to server and the return data compare to another data (for some calculation) and also send to the user Local Push Notification and all of this from the background ?
It's definitely possible to do all those things. Here's some info to start you off with location:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html
and notifications:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html
Both documents discuss how these things work in general, but also talk briefly about how they work in the background. Hopefully this gives you a jumping off point to find what you need.

iOS - Run background process while app is in suspended state

I have an application which takes characters from a web page and compares them to a UILabel in the application. What I want to do is enable the app to continue to do this even when the user has placed the app in a suspended state. All I have found online and in the documentation is the way to schedule a UILocalNotification while still in the active state. In the documentation, it states that you can declare certain permissions in the info.plist. I looked at the categories, but my application cannot really fit any of those.
Is there a way for me to continue comparing the two strings and scheduling a notification while in the background?
Thanks.
iOS doesn't really support multi-tasking in the manner you require. (i.e.: You can't simply carry on processing data in the background unless you're a very specific kind of app such as a VOIP service, etc.) otherwise you're likely to end up being terminated.
However, one possible solution would be to carry our this monitoring on a separate system (e.g.: a web server that your app interacts with) rather than within the app itself. You'd therefore:
Communicate the required string/web page to your web service
Carry out the processing on the web server.
Send a push notification to your app if the string was found on the page in question.

Would my application be accepted in the app store if it used location as a UIBackgroundMode?

I have a requirment for an application to be capable of having information pushed to it from a server while it is in the background (this information will get displayed to the user, but it should be downloaded first before the user knows it is available therefore Apple Push notifications cannot be used - because with this mechanism a) the user would get the notification from the push server b) they will accept and launch the app c) the app will do a http fetch from a server. Thus there could and probably would be a delay while the client fetches the data, however it is a requirment that as soon as the user is informed there is new data it is instantly available).
A possible way this could be achieved is if the client periodically polls the server to see if there is anything for it (yes I know that's not ideal, polling never is, but is there an alternative?).
However polling could not be implemented unless the client has the ability to execute in the background. And the only way the application can execute in the background is if its a voip/music/location/newstand application.
The application would make use of location awareness sometimes, however location is not is primary focus (the application would be a means of information being pushed/displayed to the user, some of the information, but not all, may be dependant upon location i.e. moving to some area might trigger some information to be displayed).
If this application was submitted to the app store with a UIBackgroundMode of location, would it be accepted? Does an application's main functional focus have to be location based in order to execute in the background with this mode, or is it acceptable for location to be secondary side functionality?)
I don't have an account to access it, but the App Store Review Guidelines should give you a hint. Keep in mind that they are guidelines, i.e., Apple can for any reason deny your app. No one here can definitively say if they'll take it or not.

iOS processes while in background

Is it possible to interface with a webservice (or, for what matter, do any scheduled local or remote activity) while the app is in background?
I know that you can receive remote pushes or schedule local alerts, but I wonder if I can periodically send my GPS position to a webservice even if the app is not in foreground.
I have been thinking about this myself in an attempt to impress my boss with an iPhone/iPad App that accesses our Web Service.
If the Web Service takes a non-trivial amount of time to process a request then there is absolutely no guarantee that the App won't be interrupted and stopped, therefore making it useless for any business-level tool. The only Apps that are allowed to run for extended periods in the background are a select set that use certain frameworks (music players, etc.).
I concluded that the only way of doing it is to introduce a middle tier that performs and waits for the response from the actual Web Service and provides an interface that the iOS App can poll or be pushed to in order to allow it to sleep/die whenever iOS thinks it should.
You can make sporadic network calls while you are running in the background if you are a location-based app with proper permissions. You need to make sure you are running a background task properly. FYI, there are a number of applications in the app store that do this.

Resources