Our app is using background NSURLSession to upload images. The app has data protection set to NSFileProtectionCompleteUnlessOpen.
If the device is locked when an upload completes, the didCompleteWithError is executed with the following error(I've removed the NSErrorFailingURLStringKey and NSErrorFailingURLKey.):
Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
"BackgroundUploadTask <4476D3BD-541B-4050-BFC6-0B8C6A174FE9>.<20>",
"LocalDownloadTask <4476D3BD-541B-4050-BFC6-0B8C6A174FE9>.<20>"
), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundUploadTask <4476D3BD-541B-4050-BFC6-0B8C6A174FE9>.<20>}.Although the reqeust finishes with the above error, the file is uploaded successfuly, the request's status code is 200 and the image is saved on the server side.
We looked at the device console log and just before the didCompleteWithError is called we saw this line from nsurlsessiond (I've replaced the bundle id with <bundle id>)
nsurlsessiond Couldn't set data protection class of /private/var/mobile/Containers/Data/Application/BA74D3B0-8AA4-4E0B-9BE0-011B36B9A7F0/Library/Caches/com.apple.nsurlsessiond/Downloads//CFNetworkDownload_7gxBlo.tmp, error: Error Domain=NSCocoaErrorDomain Code=257 "The file “CFNetworkDownload_7gxBlo.tmp” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/BA74D3B0-8AA4-4E0B-9BE0-011B36B9A7F0/Library/Caches/com.apple.nsurlsessiond/Downloads//CFNetworkDownload_7gxBlo.tmp, NSUnderlyingError=0x10343a300 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}It looks like the nsurlsessiond is trying to modify the data protection class or read it and fails.
We tested and changed the Caches directory data protection to NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication and the error didn't happen again.
Based on that we concluded NSFileProtectionCompleteUnlessOpen is the culprit.
How can we fix the failing request with the above error and still have data protection NSFileProtectionCompleteUnlessOpen?