4

BGTaskScheduler fails with "BGTaskSchedulerErrorDomain" - code: 1 on iOS 13.0

I have tried the sample program ColorFeed made available by Apple and it faces the same issue

let request = BGProcessingTaskRequest(identifier: "com.example.apple-samplecode.ColorFeed.db_cleaning")
request.requiresNetworkConnectivity = false
request.requiresExternalPower = true

do 
{
  try BGTaskScheduler.shared.submit(request)
} catch {
  print("Could not schedule database cleaning: \(error)")
}

The above code gets called in applicationDidEnterBackground

Earlier the task is registered in didFinishLaunchingWithOptions ...

 BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.apple-samplecode.ColorFeed.db_cleaning", using: nil) { task in

        // Downcast the parameter to a processing task as this identifier is used for a processing request.
        self.handleDatabaseCleaning(task: task as! BGProcessingTask)

 }
user12315899
  • 41
  • 1
  • 2
  • Does this answer your question? [Unable to schedule a task from within a UNNotificationServiceExtension - Error: BGTaskSchedulerErrorDomain error 1](https://stackoverflow.com/questions/58395578/unable-to-schedule-a-task-from-within-a-unnotificationserviceextension-error) – Rivera Jul 17 '20 at 15:54

2 Answers2

12

code 1 indicates BGTaskScheduler.Error.unavailable

According to the documentation:

This error usually occurs for one of three reasons:

  • The user has disabled background refresh in settings.
  • The app is running on Simulator which doesn’t support background processing.
  • The extension either hasn’t set RequestsOpenAccess to YES in The Info.plist File, or the user hasn’t granted open access.

Are you running on a real device? Is background refresh enabled on that device?

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • 1
    Thanks. Yes. Background Fetch and Processing are enabled in info.plist. And I am running on real iPhone XR. Also this is not an app extension. Surprisingly same is seen for ColorFeed app. If you can try the sample app from apple and see if it works for you then that might help. – user12315899 Nov 04 '19 at 23:46
  • https://developer.apple.com/documentation/backgroundtasks/refreshing_and_maintaining_your_app_using_background_tasks – user12315899 Nov 04 '19 at 23:46
  • 1
    Yes, that code works for me. Did you check that Background App Refresh is turned on in "Settings, General" on your device? Turning that off gives me error, code 1. – Paulw11 Nov 05 '19 at 00:45
  • Thanks! That worked!! All this while I was looking at App's background refresh setting instead of the top-level one. But on my iPhone it was disabled by default. Any idea how to enable it programmatically? – user12315899 Nov 09 '19 at 04:00
  • You can't. The user has to enable it. If apps could just turn it on, there wouldn't be much point in having it as a setting :) – Paulw11 Nov 09 '19 at 04:45
2

In my case I had to run the app in Release mode to get it to work.

Edit Scheme -> Build configuration -> Release

ahbou
  • 4,710
  • 23
  • 36