Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase storage upload works in Simulator but not on iPhone

Any reason why Xcode would complain to me about permissions in regards to uploading an image to Firebase Storage but when I run the same app in the Simulator, it works fine?

Permissions Error:

Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/IMG_0974.JPG Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0974.JPG” couldn’t be opened because you don’t have permission to view it." UserInfo={NSURL=file:///var/mobile/Media/DCIM/100APPLE/IMG_0974.JPG, NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0974.JPG, NSUnderlyingError=0x14805d680 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

like image 824
Michael Williams Avatar asked Jun 02 '16 22:06

Michael Williams


People also ask

Where does the iPhone simulator store its data?

type: ~/Library/Application Support/iPhone Simulator. The Directories are the iOS version of the different Simulators. The Sub Directories are the Apps install on the simulator.

How do I change the simulator on my iPhone?

With the simulator running go to File > Open Simulator > iOS [current version] and select the desired device.


2 Answers

I'm having the same problem right now. Uploading works fine in the Simulator, but on a device it gives me the permission error. My workaround has been to upload the image as data rather than with the URL to the file:

let imageData = UIImageJPEGRepresentation(image, 1.0)
let uploadTask = storageReference.child(remoteFilePath).putData(imageData, metadata: metadata, completion: { (metadata, error) in
    // Your code here
}
like image 144
Alex King Avatar answered Sep 24 '22 15:09

Alex King


Uploading works fine in the Simulator, but on a device it gives me the permission error. Upload the image as data rather than with the URL to the file:

Declare first

    fileprivate lazy var storageRef: StorageReference = Storage.storage().reference(forURL: "gs://yourAPP.appspot.com/")

then

     let path : String = "\(String(describing: Auth.auth().currentUser?.uid))/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(photoReference)"

                // 6

                let imageData = UIImageJPEGRepresentation(photoReference, 0.1)
                let uploadTask = self.storageRef.child(path).putData(imageData!, metadata: nil, completion: { (metadata, error) in
                    // Your code here
                    if let error = error {
                        print("Error uploading photo: \(error.localizedDescription)")
                        return
                    }

                    // 7
                    self.setImageURL(self.storageRef.child((metadata?.path)!).description, forPhotoMessageWithKey: key)
                })
like image 41
Vinay Krishna Gupta Avatar answered Sep 24 '22 15:09

Vinay Krishna Gupta