我正在将一个文件下载到一个文件夹中,我正在验证该文件确实存在于代码中,但我收到了上述错误。谁能帮我弄清楚为什么我没有权限读取这个文件?
let documentsURL = NSSearchPathForDirectoriesInDomains
(.DocumentDirectory, .UserDomainMask, true)[0]
let checkValidation = NSFileManager.defaultManager()
if (checkValidation.fileExistsAtPath(documentsURL))
{
print("FILE AVAILABLE");
}
else
{
print("FILE NOT AVAILABLE");
}
print(documentsURL)
do{
let data = try String(contentsOfFile: documentsURL as String,
encoding: NSASCIIStringEncoding)
print(data)
}
catch let error { print(error) }
Error Domain=NSCocoaErrorDomain Code=257 "The file “Documents” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/7FA4D6A9-2149-4053-BF08-22E94A00AE34/Documents, NSUnderlyingError=0x137807200 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
最佳答案
尝试
fileURL.startAccessingSecurityScopedResource()
//...
fileURL.stopAccessingSecurityScopedResource()
关于ios - 无法打开文件 “”,因为您没有查看它的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355105/