I have been stumped by this issue for quite some time now.
I am working on an app that lets you upload multiple files to the server. The app now needs to support the ability to do the same from the share extension.
I managed to implement the upload but i am facing a couple of issues.
When the user initializes the upload from the share extension, I need the ability to track the progress of the upload in the host app.
I tried to recreate the session in the host app to fetch all of the upload tasks but if i do that, when i try to share something again from the share extension, it fails with the code -996.
I was also considering storing the progress locally via Realm, and that is doable, but I need the ability to suspend or cancel the task from the host app.
here is how i create my sessions
private lazy var backgroundSession: URLSession = {
return createBackgroundURLSession(with: FileUploadService.bgSessionId)
}()
private lazy var backgroundSessionExtension: URLSession = {
return createBackgroundURLSession(with: FileUploadService.bgSessionIdExtension)
}()
private func createBackgroundURLSession(with identifier: String) -> URLSession {
let configuration = URLSessionConfiguration.background(withIdentifier: identifier)
configuration.sharedContainerIdentifier = AppConstants.appGroupName
configuration.isDiscretionary = false
configuration.allowsConstrainedNetworkAccess = false
configuration.networkServiceType = .responsiveData
configuration.shouldUseExtendedBackgroundIdleMode = true
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
return session
}
As i said, this is how I tried to get the current upload tasks from the share extension, but if I do that, i run into an issue the next time when I try to upload something from the extension.
private func updateUploadTasks() {
updateTasks(for: self.backgroundSession)
updateTasks(for: self.backgroundSessionExtension)
}
private func updateTasks(for session: URLSession) {
session.getTasksWithCompletionHandler { [weak self] _, uploadTasks, _ in
guard let self = self else { return }
print(uploadTasks)
}
}
I scoured the internet to get the anwser to my questions but with little to no luck.
Anything that could point me in the right direction would be much appreciated.
What would be the best way to implement something like this that:
- has the ability to track upload progress in the host app
- has the ability to cancel() or suspend() a task in the host app