我正在尝试从 Firebase 存储下载 Word 文档。在模拟器上一切都按预期工作。然而在我的设备上,我收到以下错误:
可选(错误域=FIRStorageErrorDomain代码=-13000“发生未知错误,请检查服务器响应。”UserInfo={object=2016年10月26日.docx,bucket=app.appspot.com,NSLocalizedDescription=发生未知错误,请检查服务器响应。, ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/tmp/bulletin, NSUnderlyingError=0x1702590b0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not allowed"}, 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))
}
那么是什么导致了这个问题以及如何解决它呢?
Update:
因此,我尝试创建该目录,但我被告知我没有权限,即使文档说我可以写入 tmp。
无法创建目录错误域=NSCocoaErrorDomain代码=513“您无权将文件“today.docx”保存在文件夹“h”中。” UserInfo={NSFilePath=/tmp/h/today.docx,NSUnderlyingError=0x1702498a0 {错误域=NSPOSIXErrorDomain代码=1“不允许操作”}}
这是创建目录的代码:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(localURL.path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
我认为这里的问题是tmp
and Documents
目录实际上并不存在于/tmp
and /Documents
(例如,看起来好像/Documents
实际上是/User/Documents
,这实际上是/private/var/mobile/Documents
, see: https://www.theiphonewiki.com/wiki/)
您需要确保根据系统认为这些目录所在的位置而不是字符串来创建文件 URL:
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"my_file"] URLByAppendingPathExtension:@"txt"];
或类似的NSDocumentDirectory
。至于为什么在模拟器上有效:我认为这是因为模拟器上的沙箱与真实设备上的工作方式不同,而且/tmp
显然是一个可以写入的有效位置(尽管可能不是您想要写入两个的位置,正如当您尝试在真实设备上执行操作时 iOS 发出嘶嘶声所证明的那样)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)