Yuri Frota
Yuri Frota

Reputation: 41

Live Activities and Upload in Background iOS

I'm developing a new feature that allows the user to keep track of a background upload through live activities.

Im using the URLSessionTaskDelegate to keep track of the task progress and it works fine when the app is in foreground, but when the app goes to background, it stop after a few seconds. The upload still continues and Im able to receive updates on it's completion through the application(_ application: UIApplication, handleEventsForBackgroundURLSession but only through that.

Also when I put the app back to foreground it updates to the correct progress so this is only happening in background.

This is the URLSessionConfiguration I'm using:

let config = URLSessionConfiguration.background(withIdentifier: "\(uploadBackgroundTaskIdentifier)")

config.isDiscretionary = false
config.allowsCellularAccess = true
config.shouldUseExtendedBackgroundIdleMode = true
config.sessionSendsLaunchEvents = true
config.timeoutIntervalForResource = 28800

Does anyone knows how should I be able to keep track of the progress even when the app is in background?

Upvotes: 1

Views: 1128

Answers (1)

Paulw11
Paulw11

Reputation: 115051

Background url sessions are processed by the system, not your app.

When your app moves from the foreground it will execute briefly in the background before being suspended.

Once your app is suspended it will be returned to the background only to receive updates via the handleEventsForBackgroundURLSession method. You can't get more find-grained updates on the upload while suspended since your app receives no execution time.

If the upload takes less than a couple of minutes, you can use beginbackgroundTaskWithExpirarionHandler to get extended background execution time.

Upvotes: 0

Related Questions