-1

What I want to do: Take a video from my image picker and upload it to the Firebase Storage and create a document in my Firestore which points at it.

What is the current problem: Xcode throws me an unknown error. It fails to upload the video to the Storage. (The console response below)

My code:

if self.fireImage == nil, description == "", self.fireURL != nil{
                                
    print(self.fireURL!)
    storageVideoRef.putFile(from: self.fireURL! as URL, metadata: nil) { (storageMetaData, error) in
    if error != nil{
        print(error!.localizedDescription)
        return
        }
        print("is in database")
            storageVideoRef.downloadURL { (url, error) in
            if let metaVideoUrl = url?.absoluteString{
                                            
            docRef.getDocument { (doc, err) in
                if let doc = doc, doc.exists{
                print("Post ID already taken")
                }else{
                                                    
                print("Post gets created")
                                                
                self.db.collection("posts").document(combined).setData([
                    "hasLiked": [],
                    "likes": self.likes,
                    "video": metaVideoUrl,
                    "postType": 6,
                    "profileImage": pfp!,
                    "time": date,
                    "uid": self.userID,
                    "username": fullname!,
                    "postID": combined,
                                                
                    ]) { err in
                    if let err = err {
                        print("Error writing document: \(err)")
                    } else {
                        print("Post Document successfully written!")
                            }
                        }
                    }
                }
            }
        }
    }
}

What the console told me:

file:///Users/janjan/Library/Developer/CoreSimulator/Devices/63A8A6CC-ED10-49C2-8C24-EC6EF9919061/data/Containers/Data/PluginKitPlugin/601B4FBB-1343-4AF0-BFF4-C44CFBF3E633/tmp/trim.B03B576D-1887-4C6F-A0AE-93689A8A5EDB.MOV
2020-07-06 17:51:42.891754+0200 LiFit[20902:2197419] Task <18AD8076-F410-4E50-997E-FF77DFF5F760>.<1> finished with error [-1] Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSErrorFailingURLStringKey=https://firebasestorage.googleapis.com/v0/b/lifit-98bf5.appspot.com/o/postVideos%2FI47GVyorQQVVsn6O29qRUavz1it2203458125?uploadType=resumable&name=postVideos%2FI47GVyorQQVVsn6O29qRUavz1it2203458125&upload_id=AAANsUmSPY8KB9XMpYvZxJqWiHPrwz4FT9uPxfKy8yyf-eIvrcRVEP0lkWDXWiSWy9i1q7hkOxdGqinJvkALDrskCIk&upload_protocol=resumable, NSErrorFailingURLKey=https://firebasestorage.googleapis.com/v0/b/lifit-98bf5.appspot.com/o/postVideos%2FI47GVyorQQVVsn6O29qRUavz1it2203458125?uploadType=resumable&name=postVideos%2FI47GVyorQQVVsn6O29qRUavz1it2203458125&upload_id=AAANsUmSPY8KB9XMpYvZxJqWiHPrwz4FT9uPxfKy8yyf-eIvrcRVEP0lkWDXWiSWy9i1q7hkOxdGqinJvkALDrskCIk&upload_protocol=resumable, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "BackgroundUploadTask <18AD8076-F410-4E50-997E-FF77DFF5F760>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundUploadTask <18AD8076-F410-4E50-997E-FF77DFF5F760>.<1>, NSLocalizedDescription=unknown error}
An unknown error occurred, please check the server response.
``
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Jan Swoboda
  • 152
  • 2
  • 15
  • 1
    It appears to be due to a negative response from the server. Do you have any particular rules on firebase? Even if it is old as an answer, try following [this solution](https://stackoverflow.com/a/41707603/9907720). – Gius Jul 06 '20 at 18:48
  • @Gius thank you for the suggestion. I tried that method and still got the same error message. I guess I have to take a look at my firebase settings (though I am able to upload photos). – Jan Swoboda Jul 07 '20 at 11:24

1 Answers1

0

Looks like it was an iOS 13 problem, where the file-path is slightly different than in all other versions.

I solved this by checking if the uploading device is on iOS 13 and then changing the file-path for it to work.

Here is the code:

else if let videoURL = info[.mediaURL] as? URL {
            fireURL = videoURL
            
            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)
                    fireURL = tempDirectoryURL.appendingPathComponent(String(urlSlices[1])).appendingPathExtension(String(urlSlices[2]))

                    //Copy the video over
                    try FileManager.default.copyItem(at: videoURL, to: fireURL!)
                }
            }
            catch let error {
                print(error.localizedDescription)
            }
Jan Swoboda
  • 152
  • 2
  • 15