NSURLSession downloads to inaccessible file

I'm using an NSURLSession for background downloads, and even though the download is apparently successful, didFinishDownloadingToURL is giving a URL that the app doesn't have permission to access. An example:


/var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/com.company.appname/CFNetworkDownload_u0dx1W.tmp


Usually, downloads are successful. In that case, the URL from didFinishDownloadingToURL looks like this:


/private/var/mobile/Containers/Data/Application/xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx/Library/Caches/com.apple.nsurlsessiond/Downloads/com.company.appname/CFNetworkDownload_u0dx1W.tmp

The big problem is that once I start getting bad URLs, nsurlsessiond seems to be stuck in that state. Failures continue even after rebooting the device or deleting and reinstalling the app. I've observed this problem with iOS 10.2 and the iOS 10.3 betas. Has anyone seen something similar and found a workaround? This really looks to me like an iOS bug.


—Chris

Replies

Using the same bundle ID do solve the problem.

Thanks for confirming that.

I've tried to use wildcard app ID

Wildcard App IDs only make sense in the context of a provisioning profile. An app always has a specific App ID.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
This bug still does not seem to be fixed. We are also getting this bug intermittently at our end. Is apple working on fixing it?

We also encountered this issue recently. Any update on if a fix for it is coming?

Thanks!
I can confirm that this bug still occurs and that Chris's solution works. Just to spell it out a bit more, the solution is:
  1. Add the App Groups entitlement to your app

  2. Add a "groups.<your identifier>" sharedContainerIdentifier to your URLSession config.