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. :-(
Here's the only way I've found to test background fetch.
- Edit your scheme
- Select Run
- Select Options
- Check the "Launch due to background fetch event" option
- Plug-In your iOS Device and run the application on it. It does not work in the iOS Simulator.
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)"
You can create your own background fetch push notification to your real device via Houston
- install Houston: Open Terminal app and use
sudo gem install houston
- create PEM certificate for your app Generate .pem file Used to setup Apple PUSH Notification
- Get your device token from
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
- 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'
This method is called.
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
you forgot to write @escaping after completionHandler
In Xcode 8.3.2 this problem doesn't exist. Debug->Simulate Background Fetch does its job. It even works on a simulator.