问题
I am trying to integrate Firebase into an iMessage extension.
As a test, I am setting up Firebase and trying to save a local file to Firebase Storage in the viewDidAppear
method. The Firebase Real-time Database works fine in the code below, only the storage part does not.
The exact same code works when done in a normal app (i.e. not a iMessage extension).
I get the following error message:
Error Domain=FIRStorageErrorDomain Code=-13000
"An unknown error occurred, please check the server response."
UserInfo={ResponseErrorDomain=NSURLErrorDomain, object=test.jpg,
bucket=myapp.appspot.com, ResponseErrorCode=-995,
`NSLocalizedDescription=An unknown error occurred, please check the server response.
I am doing the following:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
FIRApp.configure()
FIRAuth.auth()?.signInAnonymously { (user, error) in
guard let fileURL = Bundle.main.url(forResource: "test", withExtension:"jpg") else { return }
let storageRef = FIRStorage.storage().reference().child("test.jpg")
storageRef.putFile(fileURL, metadata: nil) { (metaData, error) in //produces error
if error != nil {
print(error.debugDescription)
}
}
FIRDatabase.database().reference().updateChildValues(["someKey" : "someValue"]) // works fine
}
}
回答1:
I have a suspicion that iMessage extensions may get limited access to the file system (since they live in a different sandbox than the normal app), and thus getting the file wouldn't work. In this case putData
works, but putFile
doesn't. Solution: always upload and download in memory (putData
and dataWithMaxSize:
) vs the file system (putFile
and writeFile
).
来源:https://stackoverflow.com/questions/39689929/firebase-storage-not-working-on-imessage-extension