Nonouf
Nonouf

Reputation: 398

Does background app refresh have an effect on a background URLSession?

Given I have a standard background URLSession and URLSessionDownloadTask, as below (I deliberately simplified the code for the sake of the example):

let sessionConfig = URLSessionConfiguration.background(withIdentifier: "mySessionID")

sessionConfig.isDiscretionary = false
sessionConfig.sessionSendsLaunchEvents = true

self.urlSession = URLSession(configuration: sessionConfig,
                             delegate: self,
                             delegateQueue: nil)

let task = urlSession.downloadTask(with: url)

task.resume()

It seems that enabling/disabling Background App Refresh (Settings => General => Background App Refresh) changes how URLSession and its delegate behave.

When background app refresh is on, the OS wakes up my app in background and call the delegate methods as described in Apple's documentation, which allows me for example to move my downloaded file in the Photo library. When background app refresh is off, the delegate methods are never called while the app is in background. They are however called when the app the user puts the app back in foreground.

The code running in both scenarios is the same, it implements URLSessionDownloadDelegate in a manager and application(_:handleEventsForBackgroundURLSession:completionHandler:) in the app delegate.

I can't find any official documentation from Apple stating this is true. Background app refresh is only supposed to have an effect on the Background App Refresh API (which allows you to use background fetch and background processing).

Has anyone been able to download a file in background and move it in background with Background App Refresh turned off?

Upvotes: 1

Views: 932

Answers (1)

Nonouf
Nonouf

Reputation: 398

I finally found an answer to this problem. Posting it here for anyone having the same issue.

Thanks to this thread, see Quinn's response below :

iOS 13 introduces new behaviour (r. 47718087) whereby, if the user has turned Background App Refresh off in Settings — either globally or for your app, assuming it has this setting — your app won’t be resumed (or relaunched) when the tasks complete in an NSURLSession background session. That is, your session will behave as if sessionSendsLaunchEvents were set to false.

Upvotes: 2

Related Questions