I am trying to upload an image to firebase storage from a share extension in iOS, I have authed and am communicating with the database but when I attempt to upload the file it fails straight away.
I have made sure that the code that I am using works by using it in my main app. I have also made sure that the file is being saved in the file manager prior to being uploaded correctly.
Here is the code for saving the file prior to the upload:
if let data = downsizeImage(image: image).jpegData(compressionQuality: 0.2) {
let fileManager = FileManager.default
let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.<DOMAIN>.imageShare")?.appendingPathComponent("ImageToSend.jpg")
do {
try data.write(to: url!)
}
catch {
print(error.localizedDescription)
}
}
Here is the code for the upload task:
let storageRef: StorageReference = Storage.storage().reference().child(storageLocation).child(UUID().uuidString)
var completed = false
var mediaUploadTask: StorageUploadTask?
let mediaTimeoutTask = DispatchWorkItem{ () in
if !completed {
mediaUploadTask?.cancel()
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 30, execute: mediaTimeoutTask)
mediaUploadTask = storageRef.putFile(from: mediaUrl, metadata: nil) {(metadata, error) in
completed = true
...
}
What should happen is the image is successfully uploaded and the function would continue as normal. What actually happens is the upload fails nearly straight away. Here is the error returned:
Printing description of error:
▿ Optional<Error>
- some : Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response."
UserInfo={bucket=link-ages-55880.appspot.com,
_NSURLErrorFailingURLSessionTaskErrorKey=BackgroundUploadTask <AC5EADEA-6257-4C32-9454-17626156AA15>.<1>,
object=media/4qnjSBKysi79uCR3cTzf/04D22317-D2C0-4A5C-B032-4F37DB2C8F7A,
_NSURLErrorRelatedURLSessionTaskErrorKey=(
"BackgroundUploadTask <AC5EADEA-6257-4C32-9454-17626156AA15>.<1>"
),
NSLocalizedDescription=An unknown error occurred, please check the server response.,
ResponseErrorDomain=NSURLErrorDomain, ResponseErrorCode=-995}
If anyone has any idea what the problem might be, your ideas would be greatly appreciated. Thanks.
Here is a screenshot of the debugger:

I have found out what the problem was. Due to the way that iOS sandboxing works, calling:
storageRef.putFile(from: mediaUrl, metadata: nil, completion: {(metadata, error) in})
fails. More info Here: Original Answer.
Instead calling:
storageRef.putData(Data, metadata: nil, completion: {(metadata, error) in})
worked as intended.
putFile didn’t work in an extension because GTMSessionUploadFetcher tried to use a background URLSessionConfiguration and that isn’t possible in an extension without some complicated coordination with the main app. It is especially complicated when the upload is being performed by a third party library (like Firebase).
I submitted a fix to Firebase Storage that ensures that a background session is used only when the upload is occurring in the main app (i.e. not in an extension). This fix is included in Firebase 10.24.0. In other words, nowadays, you can safely use putFile in an extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With