ios - 无法打开文件 “”,因为您没有查看它的权限

标签 ios swift

我正在将一个文件下载到一个文件夹中,我正在验证该文件确实存在于代码中,但我收到了上述错误。谁能帮我弄清楚为什么我没有权限读取这个文件?

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/

相关文章:

ios - RestKit - 将 JSON 内容保存到字符串核心数据属性中

ios - 导入带密码的证书

ios - 使用 glReadPixels 读取 Tap Point 的值? OpenGL 2.0 iOS

javascript - 如何防止在 PhoneGap 中双击移动设备滚动?

swift - 在 NavigationView 中使用函数会引发错误 : Cannot convert return expression of type 'AnyView.Type' to return type 'AnyView'

ios - Phonegap 1.3 captureImage到Photo Library并在HTML/App界面显示图像

ios - IB Designables : Failed to render and update auto layout status (Google Maps)

ios - 根据 Swift 中的选定 View 更新多个标签

swift - 从 SwiftUI 的列表中删除默认填充

ios - Kudan:ARCameraViewController 的内存释放问题