Will background fetch be called when app is in background? - ios

Will iOS call performFetchWithCompletionHandler for the running app that is taken to background (using Home button)?
I thought it is only called when iOS launches my app to do background fetch.
If iOS launches performFetchWithCompletionHandler even if my app is running in background, then I have one more question:
I need to fetch data from remote server periodically. I now use NSTimer and it works even when app is in background. My idea was to use background fetch to do it even if app is not running, but if enable background fetch, I may face situation when my own thread (with NSTimer) does some fetching and iOS calls performFetchWithCompletionHandler.
So, should I stop any timers when my app goes to background and wait for background fetch, then?

Related

iOS - Background Services when app is terminated

Do background mode functions like Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
Thanks
Yes, it works (most of the time), if you set up everything correctly and have the permissions. Your app need's to be launched at least 1 time, so it can subscribe to the updates.
For background fetch, set UIApplication.shared.setMinimumBackgroundFetchInterval(3600) at the didFinishLaunching method, implement the performFetchWithCompletionHandler method, and enable the Background Fetch in the Background Modes.
Pay attention to do it as quickly as possible, and call the completionHandler as soon as possible.
Read more on Updating Your App with Background App Refresh here
For notification updates, you must also set the allowsBackgroundLocationUpdates property of your CLLocationManager object to true, and enable the Location updates in the Background Modes.
Read more on Handling Location Events in the Background here
Background fetch works like, it allows the app to download the contents when it is background. If the app is terminated and gets some trigger to download content, it will actually wake up by doing silent-launch of the app in the background and download the contents. Please see the Apple description on this below.
Each of the preceding modes lets the system know that your app should
be woken up or launched at appropriate times to respond to relevant
events. For example, an app that begins playing music and then moves
to the background still needs execution time to fill the audio output
buffers. Enabling the Audio mode tells the system frameworks that they
should continue to make the necessary callbacks to the app at
appropriate intervals. If the app does not select this mode, any audio
being played or recorded by the app stops when the app moves to the
background.
Here, preceding modes refer to Background fetch, Audio and AirPlay, Location updates and other Background modes of the app.
Please refer Apple document on Background Execution. See Declaring Your App’s Supported Background Tasks for more info on different background modes.
Location update works differently. There are multiple Apple services available to fetch location.
Significant Location service: It works in all modes. Foreground, Background and even in terminated mode.
Standard Location service: It works only in FG and BG mode. It does not work when the app is in terminated mode.
On more details on Location in BG, please refer Handling Location Events in the Background document.
Hope it helps.
Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
It depends on which type of location service you have used in the project. Refer below analysis of all types of location services.
Standard location service: If you implemented standard location service then it will work only for background and foreground
state.
Significant location updates: If you implemented significant location updates then it will work for background, foreground and
terminate state as well.
Region Monitoring: If you implemented significant location updates then it will work for background, foreground and
terminate state as well.
Visits Location Service: If you implemented Visits Location Service then it will work for background, foreground and
terminate state as well.
Please refer below references.
Apple official doc
Raywenderlich article

Is iOS Background fetch triggered only in background mode?

I am working on the iOS app, that needs periodic downloads from the server both in active and background mode. I know that for the Background mode there is a possibility to use Background Fetch functionality. However, I did not find if Background Fetch works in active mode as well.
Can some body tell me if it does work?
Or if it doesn't, what is the best solution to get some data periodically on iOS app (basically indefinitely until the app is terminated)?
The way to achieve what you want to do is by using background fetches. (as you are doing).
As the apple documentation saids:
The system wakes the app at opportunistic moments to begin downloading
new content.
That means that your app doesn't needs to be in background mode, it will be waked up.
This method will run over arbitrary intervals, depending on how well your app behaves in terms time consumption and energy usage.

Running App in Background (iOS)

How to run whole app in background?
I'm using location service. My aim is to detect, connect and write to BLE when my app is on background state. So I have 2 option, run whole app in background or execute function when I enter background state. But when I try to call function, it doesn't worked.

Background recording and server upload

I have an iOS app that is communicating with a bluetooth peripheral. When you tap a button on the peripheral, the app should start recording, whether it's in background or foreground.
Furthermore, there will be multiple recordings, in order to send them quickly to the server. So the app will record in chunks and then send them to the Parse database that I set up.
The chunk recording and uploading parts are working fine, but I have problems while the whole process starts in background, due to the bluetooth notification. The code that starts the recording is executed, but the completion block of the upload process is not. Maybe the recording starts but never finishes because the system stops the execution, since the app is in background?
I added the audio background mode so the app will be able to record in background, and I also added the voip background mode, since I read it will enable background network activity (which is something that I need in this case).
Unfortunately this didn't work.
Any suggestions will be appreciated.

Code to run when app terminates from Suspended state

I am working on an iOS app that has Multitasking enabled.Now, I want to perform some stuff when the app quits but not when it goes to the background. I understand we have following methods:
applicationWillEnterBackground:
applicationWillEnterBackground:
These methods are always called when app goes in background not necessarily getting terminated. I don't want to run my code if its in background but not terminating.
applicationWillTerminate:
This method works great if I terminate my app while in foreground or background but (according to Apple documentation) this method only gets called only when the app terminates while it is in foreground or background but not in the suspended state.
So, I would like to know how can I make my code run when the app is terminated while in suspended mode as well?
Any help would be appreciated.

Resources