简体   繁体   English

无法将视频上传到 iOS 上的 Firebase 存储 13

[英]Can't Upload Video to Firebase Storage on iOS 13

Works perfectly fine on iOS 12.在 iOS 12 上完美运行。

Simple boilerplate code:简单的样板代码:

let storageRef = storage.reference().child("\(profile.studioCode)/\(selected.classId)/\(uploadDate)")

        //Upload file and metadata
        let uploadTask = storageRef.putFile(from: videoURL, metadata: metadata)

        //Listen for state changes and, errors, and completion of the upload
        uploadTask.observe(.resume) { (snapshot) in
            //upload resumed or started
        }

        uploadTask.observe(.pause) { (snapshot) in
            //upload paused
        }

        uploadTask.observe(.progress) { (snapshot) in
            //upload progress
        }

        uploadTask.observe(.success) { (snapshot) in
            //upload successful
        }

        uploadTask.observe(.failure) { (snapshot) in
            //upload failed
        }

Gives me:给我:

Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response."

I've updated Cocoapods and Firebase to the newest versions, tried allowing arbitrary loads, and tried signing out and back into the app to reset my auth token.我已将 Cocoapods 和 Firebase 更新到最新版本,尝试允许任意加载,并尝试注销并重新登录应用程序以重置我的身份验证令牌。 In iOS 13 it throws that error immediately on upload, but on iOS 12 it uploads perfectly fine.在 iOS 13 中,它在上传时立即抛出该错误,但在 iOS 12 中,它上传得非常好。 Any help or insight would be greatly appreciated.任何帮助或见解将不胜感激。 Thanks!谢谢!

I had a similar issue but here is an easy workaround: You need to use '.putData' instead of '.putFile' and specify the MIME type on upload.我遇到了类似的问题,但这里有一个简单的解决方法:您需要使用“.putData”而不是“.putFile”并在上传时指定 MIME 类型。

let metadata = StorageMetadata()
//specify MIME type
metadata.contentType = "video/quicktime"

//convert video url to data
if let videoData = NSData(contentsOf: videoURL) as Data? {
    //use 'putData' instead
    let uploadTask = storageRef.putData(videoData, metadata: metadata)
}

How I ended up fixing it:我最终如何修复它:

It turns out that file paths are different in iOS 13 than iOS 12:事实证明,iOS 13 中的文件路径与 iOS 12 中的文件路径不同:

iOS12 path: iOS12路径:

file:///private/var/mobile/Containers/Data/Application/DF9C58AB-8DCE-401B-B0C9-2CCAC69DC0F9/tmp/12FD0C43-F9A0-4DCB-96C3-18ED83FED424.MOV file:///private/var/mobile/Containers/Data/Application/DF9C58AB-8DCE-401B-B0C9-2CCAC69DC0F9/tmp/12FD0C43-F9A0-4DCB-96C3-18ED83FED424.MOV

iOS13 path: iOS13路径:

file:///private/var/mobile/Containers/Data/PluginKitPlugin/5DFD037B-AC84-463B-84BD-D0C1BEC00E4C/tmp/trim.7C8C6CD1-97E7-44D4-9552-431D90B525EA.MOV file:///private/var/mobile/Containers/Data/PluginKitPlugin/5DFD037B-AC84-463B-84BD-D0C1BEC00E4C/tmp/trim.7C8C6CD1-97E7-44D4-9552-431D90B525EA.MOV


Note the extra '.'注意额外的“。” in the iOS13 path.在 iOS13 路径中。 My solution was to, inside of my imagePickerController didFinishPickingMediaWithInfo function, copy the file into another temp directory, upload it from there, and then delete the copy.我的解决方案是,在我的 imagePickerController didFinishPickingMediaWithInfo function 中,将文件复制到另一个临时目录,从那里上传,然后删除副本。

 do {
            if #available(iOS 13, *) {
                //If on iOS13 slice the URL to get the name of the file
                let urlString = videoURL.relativeString
                let urlSlices = urlString.split(separator: ".")
                //Create a temp directory using the file name
                let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
                let targetURL = tempDirectoryURL.appendingPathComponent(String(urlSlices[1])).appendingPathExtension(String(urlSlices[2]))

                //Copy the video over
                try FileManager.default.copyItem(at: videoURL, to: targetURL)

                picker.dismiss(animated: true) {
                    self.videoRecorded = false
                    self.showUpload(targetURL)
                }
            }
            else {
                //If on iOS12 just use the original URL
                picker.dismiss(animated: true) {
                    self.videoRecorded = false
                    self.showUpload(videoURL)
                }
            }
        }
        catch let error {
            //Handle errors
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM