iOS 13 Schedule iOS background tasks

Viewed 12297

I am implementing BackgroundTasks Framework for updating the data. But I got the below issue

Could not schedule refreshApp: Error Domain=BGTaskSchedulerErrorDomain Code=1 "(null)"
Could not schedule data featch: Error Domain=BGTaskSchedulerErrorDomain Code=1 "(null)"
2019-10-01 19:19:32.550320+0530 SOBackgroundTask[34131:1129470] Can't end BackgroundTask: no background task exists with identifier 3 (0x3), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
9 Answers

Here are possible error codes for Domain=BGTaskSchedulerErrorDomain extracted from ObjC headers with some explanation.

BGTaskSchedulerErrorCodeUnavailable = 1 // Background task scheduling functionality is not available for this app/extension. Background App Refresh may have been disabled in Settings.

BGTaskSchedulerErrorCodeTooManyPendingTaskRequests = 2 // The task request could not be submitted because there are too many pending task requests of this type. Cancel some existing task requests before trying again.

BGTaskSchedulerErrorCodeNotPermitted = 3 // The task request could not be submitted because the appropriate background mode is not included in the UIBackgroundModes array, or its identifier was not present in the BGTaskSchedulerPermittedIdentifiers array in the app's Info.plist.

For:

BGTaskSchedulerErrorDomain error 3

Check inside the project’s .xcodeproj file for the appropriate target. Then go to the info tab and Custom iOS Target Properties and check that the permitted background task scheduler identifiers (BGTaskSchedulerPermittedIdentifiers) are added.

This solved my problem when adding BackgroundTasks to an existing project.

Please check if you miss registering BGTaskSchedulerPermittedIdentifiers in info.plist file of your project.

I have tested on real device(iOS13.2, and iOS13.2.2), but it's same result.

Error Domain=BGTaskSchedulerErrorDomain Code=2 "(null)" Can't end BackgroundTask: no background task exists with identifier 37 (0x25), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

Error Domain=BGTaskSchedulerErrorDomain Code=1 "(null)" Can't end BackgroundTask: no background task exists with identifier 113 (0x71), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

It seems that there is still exist bug.

https://forums.developer.apple.com/thread/121990

I think this is because that your mobile turn the bg refresh off!!!!!!!

In case of BGTaskSchedulerErrorDomain error 3, I didn't add below

<array>
    <string>com.shiny.job</string>
    <string>com.shiny.jobpower</string>
    <string>com.shiny.jobnet</string>
    <string>com.shiny.jobpowernet</string>
</array>```

Just check the Background Modes has been added to the target Capabilities and that Background fetch and Background processing options are selected

According to https://developer.apple.com/documentation/backgroundtasks/bgtaskschedulererrorcode/bgtaskschedulererrorcodeunavailable?language=objc, 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 keyboard extension either hasn’t set RequestsOpenAccess to YES in The Info.plist File, or the user hasn’t granted open access.
  • The extension type isn’t able to schedule background tasks.
Related