iOS - Background Services when app is terminated - ios

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

Related

Is there anyway to check when an app will be suspended?

Is there a universal time of iOS app suspension time (i.e when it goes out of background mode and terminates).
Background
The app is in the background and executing code. Most apps enter this
state briefly on their way to being suspended. However, an app that
requests extra execution time may remain in this state for a period of
time. In addition, an app being launched directly into the background
enters this state instead of the inactive state. For information about
how to execute code while in the background, see Background Execution.
Suspended
The app is in the background but is not executing code. The system
moves apps to this state automatically and does not notify them before
doing so. While suspended, an app remains in memory but does not
execute any code. When a low-memory condition occurs, the system may
purge suspended apps without notice to make more space for the
foreground app.
No, there is not any possibility to know about that, according to this:
Suspended: ...The system moves apps to this state automatically and does not notify them before doing so...
Link: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

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 continue monitoring iBeacon when screen is off in iOS?

I am developing an iOS app to monitor iBeacons. It works well in both foreground and background. I also need it to keep monitoring even when the screen is off. Now my problem is, when I turn off the screen with the shoulder button, NSLog shows that the iBeacon signal goes off (RSSI=0, beacon.accuracy=-1.0) accordingly, and 10 seconds later, there is no beacon found at all, while the delegate method locationManager:didRangeBeacons:inRegion: is called continuously. It seems that the app is running when the screen is off but iOS turns off the location service for iBeacon ranging. I tried to set "locationManager.pausesLocationUpdatesAutomatically = NO;" but still doesn't work. Is there any advice on this issue? Thanks in advance.
It seems that you're doing ranging instead of monitoring. Ranging only works when the app is active—either in the foreground or in the background. The catch is, iOS will automatically put an app to sleep a few seconds after you navigate away from it, or lock the screen.
What you have to keep in mind is, there's really no such thing as "permanent background state" on iOS. Apps run in the background for some limited time while transitioning to the suspended state (to let them, e.g., finish uploads/downloads), and there are also Background Modes that sometimes enable an app to run in the background, but these are usually reserved for very specific apps—e.g., the "audio" Background Mode means an app can be running in the background as long as it plays music, etc. There is a Background Mode for "location" services, but it's reserved for turn-by-turn navigation apps.
Beacon monitoring however continues running even if your app goes to sleep, or gets terminated by the iOS due to memory pressure. When you go in or out of range of a beacon, iOS will launch your app into the background to handle the event, giving it a few seconds (which you can extend up to a few minutes with a Background Execution Task) to do so (you can range during that time), but then it's back to sleep.
You might also want to take a look at this question:
Receive signal from beacon while app is in the background

IOS Location updates on background

I need to know if i can get the location on background mode. Could i get the battery level to work with it.
Or the apple store will reject the app because the limits of the background modes I can work with.
Click on projects.
Click on Target.
Click on Capabilities.
Click on Background Modes.
Click on Location updates.
your location delegates are called even when your app is in background mode.
Apple docs have good information about saving battery. Which location services you need to use according to your apps need.
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1
To get battery level
https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/batteryLevel
You can get the location in background mode.
And you can get battery level in background when location update is received.
But please take a look into Apple Review Guidelines.
2.16 Multitasking Apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion,
local notifications, etc.

When an iOS app directly enters the background state?

Can someone tell me a scenario when an iOS application directly enters the background state?.
Here I have quoted the lines from iOS Application Programming Document in multitasking section.
If your app is launched into the background instead—usually to handle
some type of background event—the launch cycle changes slightly to the
one shown in Figure 3-3. The main difference is that instead of your
app being made active, it enters the background state to handle the
event and then is suspended shortly afterward.
Added ...
In the iOS Application Programming Document if you see the figure 3.3 titled Launching an app into the background, the flow is like this User taps app icon -> main() -> UIApplicationMain() -> Enter background. Is there any chance when the app directly enters background when an user taps app icon. I interpreted the image like this. Is it correct?
Thanks.
One scenario for a background launch (App X)
X registered for location background mode in its Info.plist
X is run by the user, and registers for significant location changes while running
The user switches to another app Y, so X goes to background and is then suspended (it will be returned to background mode whenever there is a significant location change to handle, and then be suspended again)
The app Y eats lots of memory, so suspended applications (including X) get kicked out of memory
a significant location change comes in. Now X is launched into background.
Scenario
Lets say you had registered your application for Local/Push notification. Then your application will launch in background run some code which have written inside your applicationDidEnterBackgroud: delegate method and then it terminates immediately.
Check listing 2
Apple documentation
EDIT:
Applications might also find local notifications useful when they run
in the background and some message, data, or other item arrives that
might be of interest to the user. In this case, they should present
the notification immediately using the UIApplication method
presentLocalNotificationNow: (iOS gives an application a limited time
to run in the background). Listing 2-2 illustrates how you might do
this.

Resources