Firebase存储上传在模拟器中可用,但不能在iPhone上使用

为什么Xcode会向我抱怨关于上传图片到Firebase存储的权限,但是当我在模拟器中运行相同的应用程序,它工作正常吗?

权限错误:

正文文件无法访问:/var/mobile/Media/DCIM/100APPLE/IMG_0974.JPG错误域= NSCocoaErrorDomain代码= 257“文件”IMG_0974.JPG“无法打开,因为您没有权限查看它。 “ 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“不允许操作”}}

我现在有同样的问题。 上传工作正常在模拟器中,但在设备上,它给了我权限错误。 我的解决方法是将图像作为数据而不是URL上传到文件中:

 let imageData = UIImageJPEGRepresentation(image, 1.0) let uploadTask = storageReference.child(remoteFilePath).putData(imageData, metadata: metadata, completion: { (metadata, error) in // Your code here } 

上传工作正常在模拟器中,但在设备上,它给了我权限错误。 上传图片作为数据,而不是与该文件的URL:

首先申报

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

然后

  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) }) 

谢谢你,亚历克和每个人都回答了这个问题…我试过这个…下面的代码在didFinishPickingMediaWithInfo …我评论的代码处理时,用户从照片库中select图像,工作,非常感谢你所以我的代码是(下)

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { picker.dismissViewControllerAnimated(true,completion:nil) let image = info[UIImagePickerControllerOriginalImage] as! UIImage//<-- let imageData = UIImageJPEGRepresentation(image, 1.0) let imagePath = "photos/xxxyyyzzz.jpg"//whatever you want let metadata = FIRStorageMetadata() metadata.contentType = "image/jpeg" storageRef.child(imagePath).putData(imageData!,metadata:metadata){ (metadata, error) in if let error = error{ print("Error uploading photo: \(error)") } else{ //if there is no error the compiler will come here print("no error") //and maybe you want to use the full url or download url after success //for example you wanna have tha downloadURL... let downloadURL = metadata!.downloadURL() print("\(downloadURL)") //do whatever you want... } } } 

另一种select是将文件复制到“文档”目录(这是可写的)。 它使用了比UIImageJPEGRepresentation方法更多的代码,尽pipe如此,您仍然可能更喜欢使用该技术。 也许使用“文档目​​录”的方法,如果你真的不想改变原来的图像(但理论上,原来的和使用UIImageJPEGRepresentation 100%创build的差异应该是不可察觉的)。

 asset.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in let originalFileUrl = contentEditingInput!.fullSizeImageURL! /* Copy file from photos app to documents directory */ let fileManager = FileManager.default let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString let destinationPath = documentsDirectory.appendingPathComponent(originalFileUrl.lastPathComponent) let destinationUrl = URL(fileURLWithPath: destinationPath) do { try fileManager.copyItem(at: originalFileUrl, to: destinationUrl) } catch { print("Unable to copy file from photos to documents directory (run out of space on device?)") return } /* Create metadata etc. for Firebase... */ /* Upload file to Firebase */ fileRef.putFile(from: destinationUrl, metadata: metadata, completion: { (metadata, error) in // Remove the file from the documents directory do { try fileManager.removeItem(at: destinationUrl) } catch { print("Unable to remove file from documents directory") } }) }) 

查看Firebase存储规则。 并临时允许所有path没有身份validation。