I want to use iOS7's Background Fetch functionality in order to ping the server once a day.
I don't want to pull any new data - but I just want to know the app is still installed on the phone.
I can enable Background Fetch, set a Minimum Background Fetch Interval of 24 hours and always return UIBackgroundFetchResultNoData to the completionHandler.
I don't need it to be at an exact time each day. Once a day is more than enough.
Will Apple approve my app on the App Store?
Can I rely on the iOS to wake my app at least once a day and perform this action?
I'm afraid that if the answer will always be UIBackgroundFetchResultNoData iOS will stop waking my app.
Looks like Background Fetch and Remote Notifications WILL NOT work if your app was killed by the user.
According to the UIApplicationDelegate Protocol Reference:
However, the system does not automatically launch your app if the user
has force-quit it. In that situation, the user must relaunch your app
or restart the device before the system attempts to launch your app
automatically again.
Related
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.
Is this possible to download data when the application is terminated? I want to download data at a particular time to get new updates in my API. And if data download in the background after that if user run application again, the user has no need to wait for download new data.
If the application [server] that is serving the content has been terminated the answer is no. (There is nothing to continue writing data out to the buffer/stream)
You will need to provide more details regarding your applications concerns to determine if there is some sort of caching layer that could be implemented which would allow your downloads to be resumable in some cases.
No, you can't continue download when your app get terminated by user! Your app must require to remains in background state!!! Because if user force close app that means, he doesn't want to run it anymore. If your app is suspended by system then it can be wake up but not if it's terminated by user!
See there are two types of termination, one if user do from app switcher and another one is if iOS do. If user terminates its not possible to do any thing in background. But in case of iOS terminates your app it can continue download or background task. Check about NSURLsession background download for more info.
https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background?language=objc
https://www.raywenderlich.com/567-urlsession-tutorial-getting-started
I agreed with other answer. You can't continue download when your app get terminated by user!
But I'm not agree with you about Background app refresh.
Background app refresh
Here is Apple's definition :
After you switch to a different app, some apps run for a short period of time before they're set to a suspended state. Apps that are in a suspended state aren’t actively in use, open, or taking up system resources. With Background App Refresh, suspended apps can check for updates and new content.
And here some parts from another doc
Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content.
...
Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches.
iOS try to predict when user will launch app on next time. iOS launches the app in background to update content right before the momnent when user launch the app.
Another approach to update content is use the Push Notifications
Here is from Apple's doc
If your server sends push notifications to a user’s device when new content is available for your app, you can ask the system to run your app in the background so that it can begin downloading the new content right away.
Notification’s payload must include the content-available key with its value set to 1. This key lets iOS to know that the notification is silent.
Pushing Updates to Your App Silently.
I am working on application in which I want to run the background service after every 12 hours even the application is suspended or any state just like Facebook. And in that service, it will hit the webService and get the data. And when the data is changed after 12 hours, I will get the notification that this item is added, and when I tap that notification, i will move to the specific screen.
I have read on many forums that when the application is in background state, it will take only 3-4 minutes to terminate the app, and after that time, no event will occur.
But I have seen on Facebook, WhatsApp or any other social media app, even the app is in not running, or in suspended state, the app still gets the notification.
Please guide me what should I do.
This is not how it works.
The notifications you see from Facebook or WhatsApp apps are Push Notifications.
The app is not fetching information from the server. It's the server that "pushes" information to your user's phone.
Push notifications don't require your app to be active to work. And tapping on them will start your app and show the correct screen if handled correctly.
For running a task periodically, you can look into Background Fetch.
This works great, but iOS decides when to call it and can completely stop calling it (if the phone is on power saving or if your app is manually killed for example).
Keep in mind that in iOS, there is NO WAY to force the device to execute code if your app is suspended/killed. You might want to review your workflow.
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 have an app which needs to communicate with a server (to refresh it's data) once every 24 hours. This needs to happen even if the app is not open, nor in the background.
Ideally what I'd like is:
Every 24 hours, my server sends a push notification to iPad
This wakes up the app, and runs the code necessary to refresh the data
The notification is then discarded
Is any of this possible?
Is the app only woken up AFTER the user clicks on the notification? Or can I run some code before showing the notification?
Can I even discard the notification message?
If Push notifications are not the right way to do this, what is???
Thanks guys!
With release of iOS 7 this is finally possible using Remote/Silent notifications. They work same as Push notifications but instead of alerting the user immediately they can fire up background fetch mode and upload/download new content.
Here is simple tutorial:http://www.objc.io/issue-5/multitasking.html
This is only possible if your application is a Newsstand App, if it is, you can send a push notification with content-available: true once every 24 hours.
If it is not a newsstand app, you can instead use GPS fences to run code. It is allowed to let the user set up GPS fences if they want the app to update when the user comes close to an area (ie. their home / work). This will wake the app and you can run the background download then. Instapaper for example does this.
if the period is always going to be 24 hours you can use local notifications instead of push notifications.
your app won't be able to run any code unless the user open the push notification, push notifications are handled by the OS and your app have no control on them while it's not active or in the background you should take a look at the push notification programming guide
an alternative way to accomplish what you are trying to do, is to get the data from the server every time the app is started and the last update happened 24 hrs earlier ,
every time you contact the server save the date in NSUserDefault
every time the user opens the app check if the last server contact happened before 24 hours then refresh data
It is possible using a so called VOIP Push Notification. This notification can run code in the background, even when the app is completely closed.