32

My app has background modes enabled with Background Fetch checked and I validated the plist includes the appropriate fetch mode.

I have also configured the interval as follows:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum);
    return true;
}

And I have added the handler as follows:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // Get some new data here
    completionHandler(UIBackgroundFetchResult.NewData);
}

I tried going to Debug->Simulate Background Fetch, but no matter what it never enters the performFetchWithCompletionHandler. I also tried to run the app under a scheme that has the "Launch due to a background fetch event" option checked. Running under this scheme simply launches the application as usual in the simulator with no call to performFetchWithCompletionHandler.

Any ideas?

Thank you!

EDIT: This appears to be affecting the release version of my app as well so it may not be isolated to the simulator. I am running Swift 1.2.

EDIT 2: My bug report was just closed because it is a duplicate of another bug report outlining the same issue. There is still no information confirming the issue is isolated to the simulator.

EDIT 3: No mention of a fix in the Xcode 6.4 Beta 2 release notes. :-(

a432511
  • 1,747
  • 4
  • 25
  • 44
  • Xcode is just broken. – Tobias Apr 28 '15 at 03:54
  • @Tobias Do you mean to assert that the problem is isolated to Xcode and not an issue in release builds? Or are you just making a general statement that Xcode is... broken. :-) – a432511 Apr 28 '15 at 15:15
  • Just bellyaching. Currently, I'm trying to test Background Fetch in a debug build on a device. All it's doing is `println("Background Fetch"); completionHandler(UIBackgroundFetchResult.NewData)`. So far I haven't seen the statement in my device log. – Tobias Apr 28 '15 at 15:26
  • 1
    I've just confirmed you can test background fetch with a debug build on a device, by Editing the Scheme, selecting Run, selecting Options, and checking the "Launch due to background fetch event" – Tobias Apr 28 '15 at 15:33
  • 1
    I just put that info in an answer so I could add a visual. Hopefully it'll help someone out there. – Tobias Apr 28 '15 at 15:39
  • Ok. Good workaround. Hopefully apple will fix this soon. Thanks! – a432511 Apr 29 '15 at 00:46

5 Answers5

28

Here's the only way I've found to test background fetch.

  1. Edit your scheme
  2. Select Run
  3. Select Options
  4. Check the "Launch due to background fetch event" option
  5. Plug-In your iOS Device and run the application on it. It does not work in the iOS Simulator.

Visual of Step 2-4

Tobias
  • 4,387
  • 3
  • 24
  • 32
  • Thanks Tobias! You saved my day :)! – Stefan May 16 '15 at 15:35
  • Glad I could help. I was getting freaked out about sending out a build without testing it any. – Tobias May 16 '15 at 19:55
  • When i enabled "Launch due to background fetch" it doesn't launch application on device why this is happening? i need to manually click on app. – Avijit Nagare Jul 19 '16 at 07:00
  • @AvijitNagare It's simulating a background fetch event, which doesn't take place when the application is in the foreground. – Tobias Aug 17 '16 at 22:04
  • @AvijitNagare- to further clarify, the point of background fetch is to do work *while staying in the background*, for example downloading remote content, NOT to bring your app to foreground. This allows you to do useful tasks for user while he is busy doing something else on his device. There is NO way in iOS to FORCE your app to the foreground (and if there were, users would DELETE YOUR APP the first time it did so while they were in the middle of something important). Instead post a notification - http://stackoverflow.com/questions/22930259/how-to-bring-application-to-foreground-in-ios – ToolmakerSteve Dec 19 '16 at 20:21
3

Take a look on the XCode release note: https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html

"The Xcode menu command Simulator Background Fetch does not work.

Use the menu command in iOS Simulator instead. (20145602)"

odemolliens
  • 2,431
  • 2
  • 28
  • 33
  • 1
    WOW this look promising... I can't seem to find the menu command in the simulator though. All I see in the debug menu of the simulator is "Trigger iCloud sync". – a432511 Apr 14 '15 at 14:10
  • actually this comment doesn't help at all. I can't find the menu command either. Apple should update their references – Vinh Apr 15 '15 at 13:32
  • @VinhTran Correct. Apple fail. I filed a bug report with them but who knows how long that will take. IMO it is completely unacceptable to ship an IDE/SIM that doesn't allow testing of core functionality. I think the problem is deeper than just the simulator as I am starting to see behavior in my app that would suggest background fetch is not working in the release build either. – a432511 Apr 15 '15 at 14:10
3

In Xcode 8.3.2 this problem doesn't exist. Debug->Simulate Background Fetch does its job. It even works on a simulator.

Artem Stepanenko
  • 3,123
  • 2
  • 23
  • 51
3

This method is called.

 func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

}

you forgot to write @escaping after completionHandler

2

You can create your own background fetch push notification to your real device via Houston

  1. install Houston: Open Terminal app and use sudo gem install houston
  2. create PEM certificate for your app Generate .pem file Used to setup Apple PUSH Notification
  3. Get your device token from

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

  1. run Terminal and use

for usual push notification: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -m "Hello from the command line!"

for background fetch push notification: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -n

More options for apn app:

  c.option '-m', '--alert ALERT', 'Body of the alert to send in the push notification'
  c.option '-b', '--badge NUMBER', 'Badge number to set with the push notification'
  c.option '-s', '--sound SOUND', 'Sound to play with the notification'
  c.option '-y', '--category CATEGORY', 'Category of notification'
  c.option '-n', '--[no]-newsstand', 'Indicates content available for Newsstand'
  c.option '-d', '--data KEY=VALUE', Array, 'Passes custom data to payload (as comma-delimited "key=value" declarations)'
  c.option '-P', '--payload PAYLOAD', 'JSON payload for notifications'
  c.option '-e', '--environment ENV', [:production, :development], 'Environment to send push notification (production or development (default))'
  c.option '-c', '--certificate CERTIFICATE', 'Path to certificate (.pem) file'
  c.option '-p', '--[no]-passphrase', 'Prompt for a certificate passphrase'
Community
  • 1
  • 1
Vitalii Gozhenko
  • 8,319
  • 2
  • 42
  • 64