What's the best approach to use background transfer to upload assets from gallery?
Seems like uploadTaskWithRequest:fromData:
doesn't work with NSURLSession
created with backgroundSessionConfiguration
since it causes an exception: "Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks in background sessions must be from a file". Judging from the exception description background transfer shouldn't work with streamed upload tasks too.
Fair enough. uploadTaskWithRequest:fromFile:
is the first thing that comes to mind when you think about uploading data that you already have on the disk. But the NSURLSession Class Reference tells us that this method works only with file urls, but when you get asset's url path it starts from "assets-library://asset/..." and providing this path doesn't work too.
So it seems that the only option left is to copy file from the assets library into a temporary directory, and provide its file url to uploadTaskWithRequest:fromFile:
. But it doesn't make any sense because you already have asset file saved on your disk.
Am I missing something?
Update:
Talked with Apple engineer at Tech Talks event and he confirmed that background NSURLSession supports only file urls. So indeed, you need to copy asset library into a temporary directory, and provide its file url to uploadTaskWithRequest:fromFile:
to upload it with background NSURLSession.
This behavior may change in the future though.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…