I am building an iOS application in SWIFT where user location is fetched and sent to the server after every 30 mins. When the app is opened it keeps sending the location lat/long to the server but when the app is closed or suspended it won't send. I want to send the location even the app is closed. Is this somehow possible? I just want to send the location to server no need to open the app again and again?
You app needs to support Background Tasks for being able to run in background.
I believe this has been answered multiple times. Here is the documentation link
Since you are using location, I would recommend you to use UIBackgroundModes as locations.But be sure you don't drain the users battery.
Also usage of background fetch (UIBackgroundModes - fetch) and silent notification can be a great way to get you app running after equal interval of time.
Update
For performing operations you can use silent notification - refer this apple documentation
As per documentation - the system wakes your app in the background and calls the application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
Update 2
For local notification along with scheduling custom action refer this apple documentation. But I really doubt this will help you achieve your desired result. Mainly because you app would get an event only when the user performs any action on your local notification and if he ignores it, you app will remain in background.
Related
I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.
I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.
Let me explain in details what do I want my application to do.
Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.
Could someone propose a solution how to implement this idea in iOS app?
On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).
I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.
There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs
The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:
Silent notifications are not meant as a way to keep your app awake in the background, nor are they meant for high priority updates. APNs treats silent notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.
Silent pushes are described in Apple's push notification docs.
iOS Background Execution Limits
Questions
How do I keep my app running continuously in the background?
If I schedule a timer, how do I get it to fire when the screen is locked?
How do I run code in the background every 15 minutes?
How do I set up a network server that runs in the background?
How can my app provide an IPC service to another one of my app while it’s in the background?
Answer from Apple:
The short answer to all of these is You can’t. iOS puts strict limits on background execution. Its default behavior is to suspend your app shortly after the user has moved it to the background; this suspension prevents the process from running any code.
Official: https://developer.apple.com/forums/thread/685525
I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.
I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.
Let me explain in details what do I want my application to do.
Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.
Could someone propose a solution how to implement this idea in iOS app?
On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).
I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.
There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs
The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:
Silent notifications are not meant as a way to keep your app awake in the background, nor are they meant for high priority updates. APNs treats silent notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.
Silent pushes are described in Apple's push notification docs.
iOS Background Execution Limits
Questions
How do I keep my app running continuously in the background?
If I schedule a timer, how do I get it to fire when the screen is locked?
How do I run code in the background every 15 minutes?
How do I set up a network server that runs in the background?
How can my app provide an IPC service to another one of my app while it’s in the background?
Answer from Apple:
The short answer to all of these is You can’t. iOS puts strict limits on background execution. Its default behavior is to suspend your app shortly after the user has moved it to the background; this suspension prevents the process from running any code.
Official: https://developer.apple.com/forums/thread/685525
Im developing an app and I want to have a feature that sends push notification when user leaves predefined region. For tracking the process of leaving Im gonna use CLCircularRegion and Parse for notifications. The question is - can the app execute methods of sending push when it is in background state? I know that there is a permission available, which allows to monitor location update always and I also know about delegate, which can be used on location update.
The actual question is does it really work when app in background state? The book iOS 8 Swift Programming Cookbook says that location delegate messages are not delivered normally to application, but instead they are delivered in a batch when application again becomes the foreground application. Otherwise, the Apple documentation says that the app becomes awake when location changes.
Say, user terminated app day before and on the next day he left region defined in CLCircularRegion. Will delegate trigger and fire up the method of sending push?
Region monitoring will trigger updates even if the app is in the background. Your app could respond to the region monitoring callbacks didEnterRegion:, didExitRegion: to then trigger the desired push notifications as appropriate.
Per Apple:
In iOS, regions associated with your app are tracked at all times, including when the app isn’t running. If a region boundary is crossed while an app isn’t running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it’s woken up and given a short amount of time (around 10 seconds) to handle the event. When necessary, an app can request more background execution time using the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class.
More info here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1
I wrote application for Android that listens on incoming Calendar events and triggers my flow when application in background.
Now I try to do the same for iOS.
I read a lot of stuff and ran some demos that use EKEventStore and background fetch.
However this approach does not satisfy me.
Is there other way to do that?
[EDIT]
Application uses public API only.
Thanks,
The short answer is you can't. Apple will not allow any app to do things like this in the background, as it requires turning the CPU on when it would otherwise be turned off.
When your app is running, you can read all events in the near future (say, two weeks out?) and setup a "local" push notification for the event time.
Also, if the user launches your app fairly often then Apple will allow your app to refresh using Background App Refresh. The device will typically do this in the morning before whatever time the user usually wakes up and turns their phone on for the first time each day, and throughout the day also, based on the user's activity. You can do anything you want during the background app refresh.
Another spanner in the works is how Apple's full disk encryption works. If the phone is locked, there really isn't much your app can do. Most data on the phone is encrypted with the user's passcode. Until the user enters their passcode not much can be done. In this case background app refresh will run as soon as they enter their passcode, but before they launch your app.
Maybe you could have a remote server send push notifications to the phone as well. Not sure if this is an option for you at all or not. Obviously this means the server would need access to the user's calendar data.
In general, the Android model of background execution doesn't apply to iOS.
You can register for calendar events as shown below, but your application will only be notified when it is running. On iOS, that means it is either in the foreground, recently backgrounded and still running, or has been launched/woken up by the system as part of a designated background mode (e.g. background fetch, location updates, VoIP, etc.).
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveEventStoreChangedNotification:)
name:EKEventStoreChangedNotification
object:eventStore];
If your application has a need for one of the designated background modes, you will at least get notifications from time to time (when you are woken up). I've seen approved apps use location updates just to get more execution time, of course YMMV.
I was wondering how some existing apps refresh their application data while the app isn't running. Lets say we have a rain alarm application. This application send you a (local?) notification when its about to rain in your current location or location you've manually entered. What's the best way to achieve something like this?
Is the application getting weather data on the device itself (even when the application is killed) to send a local notification when its about to rain? Doesn't this method drains the battery of my device?
Or do I need to create a standalone application which runs 24/7 and always checks the weather? It then searches a database to see who's device needs a remote notification.
Other methods are also appreciated.
In iOS6 and before, your app cannot run all the time you want in the background, the system will stop the app after a few seconds.
A way that your example can be achieved is sending push notifications from a remote server.
Note: In iOS7 it will be possible to wake you app when a notification is received and download content.