Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 Background Fetch When the App is Not Running

I've written a simple application in order to test and monitor how the background fetch feature works in iOS7.

First of all, I've set UIBackgroundMode options in my Info.plist file.

enter image description here

Then;

I added to below code into application:didFinishLaunchingWithOptions: method in AppDelegate.m:

enter image description here

At last, I've implemented the application:(UIApplication *)application performFetchWithCompletionHandler: method as requested.

enter image description here

Every time I click Debug->Simulate Background Fetch button, it changes the application's batch number and works as expected.

However, I've never been able to make it work when the application is not running (not even in the background mode, just not running).

Apple says that when the application is not running and OS wants to execute the following methods respectively:

application:(UIApplication *)application didFinishLaunchingWithOptions:
applicationDidEnterBackground:(UIApplication *)application
application:(UIApplication *)application performFetchWithCompletionHandler:

So my question is that is there a way to test background fetch when the app is not running?

I am testing this on my iPod 5th Gen with iOS 7.1.

like image 478
Ozgur Vatansever Avatar asked Mar 24 '14 11:03

Ozgur Vatansever


1 Answers

Edit 2: Note that if a user kills the app from the app switcher, the background fetch will never happen again. This is working as Apple intended. See the related "Understanding When Your App Gets Launched into the Background" docs for details:

In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user.

And an old developer forums post (the link unfortunately no longer exists)...

"If you kill an app from the multitasking UI, the system will never automatically launch the app again. The logic here is that, if the user has killed your app, they probably want it to stay dead."

You could make a simple GET request, eg: "http://dev.example.org/?ping=1" to your web server then grep the access.log on the web server for "?ping=1"

Edit: Another method to test the app being launched in the background (eg: without being able to double tap the home button and switch to it) is by creating a background-only scheme. Go to Product -> Scheme -> Manage Schemes and then duplicate your default scheme.

scheme settings

Then edit the new scheme and click the Options tab and check "Launch due to a background fetch event" - when you run your app it'll be launched directly into the background, as it would through normal use.

launch with background fetch

like image 105
taber Avatar answered Sep 17 '22 11:09

taber