Understanding the `backgroundRefreshStatus` property - ios

I've an iOS app enabled for updating locations in Background Mode. I'm listening for the UIApplicationBackgroundRefreshStatusDidChangeNotification notification, and checking the backgroundRefreshStatus property. About the property, it is said in the documentation:
This property reflects whether the app can be launched into the background to handle background behaviors, such as processing background location updates and performing background fetches.
I made some tests running the app in the simulator and, when I disable the "Background App Refresh" feature for the app in "Settings", I do receive the notification of the status change, but I see that locations keep being updated. I expected all background tasks to stop when "Background App Refresh" disabled by user... is that a simulator concern/bug? Or is the developer's responsibility to stop the background tasks anyway?
Thanks

Related

How to do a request in background each 5 minutes in iOS

I have an iOS application that communicate with an IoT gadget
And now I need a to do a request for an API each 5 minutes when the iOS app enters in background
I know this is horrible for battery consuming but in this specific case this is irrelevant since my iPhone device will always be connected to charger, and I won’t need this app in the store, so the apple politics is not a problem too
I already tried use Timer and TimerInterval, with the background state activated but none of then execute after the time I defined
Has any way to do this ?
Edit: I all my attempts I already activated background fetch, background music and all background modes
First you need to start background mode in your project. It looks like this:
To get to the background modes capability list you:
Select the project from the Project navigator.
Click the app target.
Select the Capabilities tab.
Turn the Background Modes switch on.
If you want to perform background task then select it from capability.
For more clarity for background fetch you can check below link
https://www.ioscreator.com/tutorials/background-fetch-ios-tutorial

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

How to get screen lock/unlock events on the iPhone when app is suspended?

I have a requirement where i need to track the iphone device state like if device is locked or unlocked. I was able to track these events when the app is running in foreground or background. But i also need to track the same when the app is suspended. Something like tracking the user location in background when the app is suspended. But i don't need to track the user location but only the device state.
Please suggest me some steps to solve this issue in objective-c.Thanks in advance.
You can not perform any operations once your app is in suspended state and you can not prevent your app from getting suspended unless you are using one of the background capability mode mentioned in this apple doc
So what you are looking for is not possible if you are not using either of background modes allowed by apple.
EDIT
Even if you go on and enable one of background mode like background audio, your app is likely to be rejected during review process as reviewer will see you do not have a valid reason to use that particular background mode.

How to send battery updates to LE accessory from app in background when iPhone operate in peripheral mode?

I'm developing a BLE enabled App. In my App iPhone operate in peripheral mode with Bluetooth accessories. Everything works fine until I go to background Mode Because my Phone is working as peripheral so It also update accessory with battery level it doesn't work in background because app will only wakeup once accessory will send request because its already subscribe for the update I somehow have to wakeup my app in background mode and send update let's say every x minutes .
I have already enable Bluetooth LE accessory background mode.
Apparently enabling background mode only gives the 'right' to run in the background whenever there is an event triggered.
One of the method is what you already did by 'polling' from the accessories periodically by sending request to wake up the app and execute the task.
Another way is to use background refresh. The only downside of background refresh is the event will not occur at specific period. It was mentioned at about 10-15mins typically or it might tag along with other app background wake up. If the update is not very frequent or crucial, this method would probably better for your case.
You can keep your app running in background by setting the "required background modes" option in your plist. Select "App communicate with accessory".
Please see the attached image for reference

iPad app become inactive after locking screen

In my application, I'm downloading and uploading data at some point of time on a separate thread using NSOperation. Both process take approx. 2-3 min. If application remains active then everything works fine. But if the iPad screen is locked (automatically or manually), the app becomes inactive and download/upload get paused and when screen is unlocked after some time process failed with timeout error.
I have "App plays audio" for "Required background modes" in plist file. Do I need to set any other flag for running app while screen is locked?
Please help me what is going wrong.
When the screen is locked, your app becomes inactive. Per "Responding to Interruptions" in iOS App Programming Guide:
Pressing the Sleep/Wake button is another type of interruption that causes your app to be deactivated temporarily. When the user presses this button, the system disables touch events, moves the app to the background but sets the value of the app’s applicationState property to UIApplicationStateInactive (as opposed to UIApplicationStateBackground), and finally locks the screen.
You can get extra time for background tasks according to "Executing a Finite-Length Task in the Background" in the same guide:
Apps that are transitioning to the background can request an extra amount of time to finish any important last-minute tasks. To request background execution time, call the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class. If your app moves to the background while the task is in progress, or if your app was already in the background, this method delays the suspension of your app. This can be important if your app is performing some important task, such as writing user data to disk or downloading an important file from a network server.
You can refer to the document to implement a finite-length download task in the background.
Other references:
Download data in background with iOS4
iOS Application Background Downloading

Resources