下载 Firebase 存储文件设备问题
Downloading Firebase Storage Files Device Issue
我正在尝试从 Firebase 存储中下载 word 文档。在模拟器上,一切都按预期工作。然而在我的设备上,我收到以下错误:
Optional(Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response." UserInfo={object=26 October 2016.docx, bucket=app.appspot.com, NSLocalizedDescription=An unknown error occurred, please check the server response., ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/tmp/bulletin, NSUnderlyingError=0x1702590b0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}, ResponseErrorCode=513})
我一直在看的其他帖子似乎没有给我一个有效的答案,我所知道的是文件权限存在问题,即使我使用的是推荐目录 (tmp)。
这是下载文件的代码
let Ref_Bulletin = Bulletin.referenceForURL("gs:/app.appspot.com/Bulletin/\(Today.stringFromDate(NSDate())).docx")
// Create local filesystem URL
let localURL: NSURL! = NSURL(string: "file:///tmp/today.docx")
// Download to the local filesystem
let downloadTask = Ref_Bulletin.writeToFile(localURL) { (URL, error) -> Void in
if (error != nil) {
print(error.debugDescription)
// Uh-oh, an error occurred!
} else {
print("Working As Expected")
self.Web_View.loadRequest(NSURLRequest(URL: localURL))
}
那么是什么导致了这个问题,我该如何解决?
更新:
所以我尝试创建目录,但有人告诉我我没有权限,即使文档说我可以写入 tmp。
Unable to create directory Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “today.docx” in the folder “h”." UserInfo={NSFilePath=/tmp/h/today.docx, NSUnderlyingError=0x1702498a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
这是创建目录的代码:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(localURL.path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
我认为这里的问题是 tmp
和 Documents
目录实际上并不存在于 /tmp
和 /Documents
(例如,它显示为虽然 /Documents
实际上是 /User/Documents
,实际上是 /private/var/mobile/Documents
,请参阅:https://www.theiphonewiki.com/wiki/)
您需要确保根据系统认为这些目录所在的位置创建文件 URL,而不是字符串:
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"my_file"] URLByAppendingPathExtension:@"txt"];
或 NSDocumentDirectory
类似。至于为什么这在模拟器上有效:我认为这是因为模拟器上的沙盒与真实设备的工作方式不同,并且 /tmp
显然是您可以写入的有效位置(尽管可能不是一个你想写两个,正如 iOS 当你尝试在真实设备上这样做时发出嘶嘶声所证明的那样。
我的情况是这样的;
我试图在存储引用上使用 firebase 的 write(toFile: myLocalURL)
方法,但是 myLocalURL 是在文档目录上生成的,例如;
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
.appendingPathComponent("\(fileName).\(fileFormat)")
这就是问题所在,所以我需要先write(toFile: temporaryLocation)
到一个临时位置,然后移动到所需的目的地。
FileManager.default.temporaryDirectory
.appendingPathComponent("\(fileName).\(fileFormat)")
我正在尝试从 Firebase 存储中下载 word 文档。在模拟器上,一切都按预期工作。然而在我的设备上,我收到以下错误:
Optional(Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response." UserInfo={object=26 October 2016.docx, bucket=app.appspot.com, NSLocalizedDescription=An unknown error occurred, please check the server response., ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/tmp/bulletin, NSUnderlyingError=0x1702590b0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}, ResponseErrorCode=513})
我一直在看的其他帖子似乎没有给我一个有效的答案,我所知道的是文件权限存在问题,即使我使用的是推荐目录 (tmp)。
这是下载文件的代码
let Ref_Bulletin = Bulletin.referenceForURL("gs:/app.appspot.com/Bulletin/\(Today.stringFromDate(NSDate())).docx")
// Create local filesystem URL
let localURL: NSURL! = NSURL(string: "file:///tmp/today.docx")
// Download to the local filesystem
let downloadTask = Ref_Bulletin.writeToFile(localURL) { (URL, error) -> Void in
if (error != nil) {
print(error.debugDescription)
// Uh-oh, an error occurred!
} else {
print("Working As Expected")
self.Web_View.loadRequest(NSURLRequest(URL: localURL))
}
那么是什么导致了这个问题,我该如何解决?
更新:
所以我尝试创建目录,但有人告诉我我没有权限,即使文档说我可以写入 tmp。
Unable to create directory Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “today.docx” in the folder “h”." UserInfo={NSFilePath=/tmp/h/today.docx, NSUnderlyingError=0x1702498a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
这是创建目录的代码:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(localURL.path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
我认为这里的问题是 tmp
和 Documents
目录实际上并不存在于 /tmp
和 /Documents
(例如,它显示为虽然 /Documents
实际上是 /User/Documents
,实际上是 /private/var/mobile/Documents
,请参阅:https://www.theiphonewiki.com/wiki/)
您需要确保根据系统认为这些目录所在的位置创建文件 URL,而不是字符串:
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"my_file"] URLByAppendingPathExtension:@"txt"];
或 NSDocumentDirectory
类似。至于为什么这在模拟器上有效:我认为这是因为模拟器上的沙盒与真实设备的工作方式不同,并且 /tmp
显然是您可以写入的有效位置(尽管可能不是一个你想写两个,正如 iOS 当你尝试在真实设备上这样做时发出嘶嘶声所证明的那样。
我的情况是这样的;
我试图在存储引用上使用 firebase 的 write(toFile: myLocalURL)
方法,但是 myLocalURL 是在文档目录上生成的,例如;
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
.appendingPathComponent("\(fileName).\(fileFormat)")
这就是问题所在,所以我需要先write(toFile: temporaryLocation)
到一个临时位置,然后移动到所需的目的地。
FileManager.default.temporaryDirectory
.appendingPathComponent("\(fileName).\(fileFormat)")