Background Modes, ScreenTime API

I’m attempting to run Apple’s ScreenTime API while my app is in the background

When debugging on a device my background restrictions functionality works, but on TestFlight it does not. This is the error message when analyzing the device in XCodes Console

BackgroundTaskSuspended. Background entitlement: NO

When the proper entitlements are added this error doesn't allow for the build to be uploaded to TestFlight.

Provisioning profile "iOS Team Provisioning Profile: com.xxx.xxx" doesn't include the UIBackgroundModes entitlement.

I have the proper entries in the “Signing and Capabilities” of the project, but my functionality will not run in the background when the app is not launched. I just need a function to run that calls Apple's ScreenTime API while my app is in the background. Other apps have achieved the functionality I’m looking for so I know it’s possible, but this is the roadblock I’m running into.

Answered by DTS Engineer in 853069022

You seem to be hitting a common issue and your attempt to fix that issue has made things worse. Lemme explain…


When debugging on a device my background restrictions functionality works, but on TestFlight it does not.

You have to be careful when debugging code that runs in the background because the debugger prevents your app from being suspended. So, a common cause of problems like this is:

  1. You write your code.
  2. And test in Xcode.
  3. And it all works, because your app never gets suspended.
  4. You then create a TestFlight build.
  5. And test that.
  6. And your code fails because the app is now being suspended.

The good news here is that you don’t need to use TestFlight to investigate problems like this. Rather, run your app from Xcode, then stop it, then run it again from the Home screen. This allows your app to be suspended as it normally would.

The downside is that you can’t use the debugger to debug. I have two answers to that:

  • Debug the bulk of your code using unit tests.
  • Use the system log to investigate issues related to how your code integrates with the system.

See Your Friend the System Log for lots of hints and tips about the latter.


When the proper entitlements are added this error doesn't allow for the build to be uploaded to TestFlight.

Right. The reason for that is that UIBackgroundModes is an Info.plist property and you’ve added it to your .entitlements file. Don’t do that. Rather, use Xcode’s Signing & Capabilities editor to add the Background Modes capability to your target. It’ll put the values you select in the right place.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

You seem to be hitting a common issue and your attempt to fix that issue has made things worse. Lemme explain…


When debugging on a device my background restrictions functionality works, but on TestFlight it does not.

You have to be careful when debugging code that runs in the background because the debugger prevents your app from being suspended. So, a common cause of problems like this is:

  1. You write your code.
  2. And test in Xcode.
  3. And it all works, because your app never gets suspended.
  4. You then create a TestFlight build.
  5. And test that.
  6. And your code fails because the app is now being suspended.

The good news here is that you don’t need to use TestFlight to investigate problems like this. Rather, run your app from Xcode, then stop it, then run it again from the Home screen. This allows your app to be suspended as it normally would.

The downside is that you can’t use the debugger to debug. I have two answers to that:

  • Debug the bulk of your code using unit tests.
  • Use the system log to investigate issues related to how your code integrates with the system.

See Your Friend the System Log for lots of hints and tips about the latter.


When the proper entitlements are added this error doesn't allow for the build to be uploaded to TestFlight.

Right. The reason for that is that UIBackgroundModes is an Info.plist property and you’ve added it to your .entitlements file. Don’t do that. Rather, use Xcode’s Signing & Capabilities editor to add the Background Modes capability to your target. It’ll put the values you select in the right place.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I've followed these links closely to troubleshoot, but still have had no luck.

- Entitlements

Is enabling Background Modes and having Family Controls (distrubution) enough to successfully trigger the Apple ScreenTime API while my application is in the background?

Background Modes, ScreenTime API
 
 
Q