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"}}
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.
With the simulator running go to File > Open Simulator > iOS [current version] and select the desired device.
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
}
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)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With