I was sure that I've posted here the solution, but better later than never...
Since it's a pretty long code, I've made a gist for it (hopefully it doesn't missing any custom helper): https://gist.github.com/Idomo/ece6683348372b54e470fd630b1fb76e
Beside that, to recreate the saved unfinished sessions, so it'll fire urlSession(_:task:didCompleteWithError:) and re-upload, you should call this function in application(_:, didFinishLaunchingWithOptions:):
func recreateFilesUploadTasks() {
if let filesUploadTasks = UserDefaults.standard.stringArray(forKey: UserDefaultsKeys.filesUploadTasks) {
let filesManager = FilesManager()
for sessionId in filesUploadTasks {
filesManager.recreateSession(id: sessionId)
}
}
}