-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
Description
[REQUIRED] Step 1: Describe your environment
- Xcode version: Version 11.5 (11E608c)
- Firebase SDK version: Version 6.25.0
- Firebase Component: Auth, Core, Firestore, Storage, Functions
- Component version: complete Pod.lock file is at https://drive.google.com/file/d/1GIz_tK5zr67hTvBz1dpSUJ7f6c1sj0A-/view?usp=sharing
- Installation method: CocoaPods
- My iPhone: iPhone 7, iOS 13.4.1
[REQUIRED] Step 2: Describe the problem
Steps to reproduce:
My app let user picks a file from UIDocumentPickerViewController and upload to Firebase storage. However:
- If I picked images,
.txtor.jsonfiles, they can be uploaded successfully to Firebase Storage - If I selected a custom file like
Home Improvement.numbersfile fromiCloud Drive/Numbersfolder, the upload failed with below error. This happened with other extensions like.noto.
Task .<1> finished with error [100] Error Domain=NSPOSIXErrorDomain Code=100 "Protocol error" UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey=BackgroundUploadTask .<1>, _kCFStreamErrorDomainKey=1, NSErrorPeerAddressKey={length = 28, bytes = 0x1c1e01bb 00000000 24046800 40050801 ... 0000200a 00000000 }, _kCFStreamErrorCodeKey=100, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"BackgroundUploadTask .<1>",
"LocalUploadTask .<1>"
)}
Relevant Code:
Pick file from UIDocumentPickerViewController:
func pickFile() {
let filePicker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
filePicker.allowsMultipleSelection = false
filePicker.delegate = self
present(filePicker, animated: true)
}Upload file to Firebase storage
static func uploadFile(file: FileMeta) -> StorageUploadTask? {
// Check if user is authenticated
guard let currentUser = Auth.auth().currentUser else {
return nil
}
// Check if the file exists
let fileUrl = getLocalFileUrl(fileId: file.id)
if !FileManager.default.fileExists(atPath: fileUrl.path) {
return nil
}
// Check if existing file is being uploaded
if let uploadTask = uploadTasks[file.id] {
return uploadTask
}
// Upload file
let imageRef = Storage.storage().reference(withPath: "users/\(currentUser.uid)/\(file.id)")
let uploadTask = imageRef.putFile(from: fileUrl, metadata: nil) { (_, error) in
// Mark current task had finished
uploadTasks.removeValue(forKey: file.id)
if error == nil {
// Keep track of downloaded URL
imageRef.downloadURL(completion: { (url, err) in
if err != nil {
print("downloadURL error", err!)
return
}
print("downloadURL", url!.absoluteString)
file.downloadURL = url!.absoluteString
try? DataStore.shared.saveFile(file)
})
}
}
// Keep track of running tasks
uploadTasks[file.id] = uploadTask
return uploadTask
}