Trying to use file importer in a swift app to upload documents into firebase Storage and getting error when running on live device but works perfectly fine on emulator. error:
BackgroundSession <***...> Failed to issue sandbox extension for file
file:///private/var/mobile/Library/Mobile%20Documents/comappleCloudDocs/Sample.pdf, errno = [1: Operation not permitted]
UNKNOWN ERROR Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response." UserInfo={object=Repository/Sample.pdf, ResponseBody=Can not finalize upload. Current size is 0. Expected final size is 2665098., bucket=bucket, data={length = 77, bytes = 0x43616e20 6e6f7420 66696e61 6c697a65 ... 32363635 3039382e }, data_content_type=text/plain; charset=utf-8, NSLocalizedDescription=An unknown error occurred, please check the server response., ResponseErrorDomain=com.google.HTTPStatus, ResponseErrorCode=400}
I tried separating all the logic from selectedFile.startAccessingSecurityScopedResource()
all the way through the end into a function of its own using @MainActor based on another post I found around here and issue persisted..
Below is the implementation:
.fileImporter(
isPresented: $fileImporterIsPresented,
allowedContentTypes: [.pdf],
allowsMultipleSelection: false
) {
result in
switch result
{
case .success(let url):
print(url)
guard let selectedFile:URL = url.first else { return }
guard selectedFile.startAccessingSecurityScopedResource() else
{
return
}
let firebaseRepository = Storage.storage().reference().child("Repository/Sample.pdf")
let uploadTask = firebaseRepository.putFile(from: selectedFile)
uploadTask.observe(.progress)
{
snapshot in
// Upload reported progress
percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
/ Double(snapshot.progress!.totalUnitCount)
}
uploadTask.observe(.success)
{
snapshot in
selectedFile.stopAccessingSecurityScopedResource()
uploadTask.removeAllObservers()
print("SUCCESS")
// Upload completed successfully
}
uploadTask.observe(.failure)
{
snapshot in
if let error = snapshot.error as? NSError
{
switch (StorageErrorCode(rawValue: error.code)!)
{
case .objectNotFound:
print("NOT FOUND")
// File doesn't exist
break
case .unauthorized:
print("UNAUTHORIZED")
// User doesn't have permission to access file
break
case .cancelled:
print("CANCELLED")
// User canceled the upload
break
/* ... */
case .unknown:
print("UNKNOWN ERROR \(error.description)")
print("UNKNOWN ERROR \(error.localizedDescription)")
print("UNKNOWN ERROR \(error.underlyingErrors)")
// Unknown error occurred, inspect the server response
break
default:
print("UNSPECIFIED ERROR")
// A separate error occurred. This is a good place to retry the upload.
break
}
}
}
case .failure(let error):
print(error)
}
}