Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The file “ ” couldn’t be opened because you don’t have permission to view it

Tags:

ios

swift

I am downloading a file to a folder and I am validating that the file is indeed there in the code, but I am getting the above error. Can someone help me figure out why i dont have permissions to read this file?

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"}}

like image 930
user3395936 Avatar asked Apr 01 '16 11:04

user3395936


People also ask

How do you fix you don't have permission to open this File?

a) Right-click on the file which you're unable to access and select Properties. b) Click on 'Security' tab and under 'Group or user names' click on 'Edit'. c) Click on 'Add' and type 'everyone'. d) Click on 'Check names' and then click 'OK'.

Why is my Mac saying I don't have permission to view files?

If you don't have permission to open a file or folder, you may be able to change the permissions settings. On your Mac, select the item, then choose File > Get Info, or press Command-I. Click the arrow next to Sharing & Permissions to expand the section.


2 Answers

Try

fileURL.startAccessingSecurityScopedResource()
//...
fileURL.stopAccessingSecurityScopedResource()
like image 146
Ivan Vavilov Avatar answered Sep 28 '22 09:09

Ivan Vavilov


Your documentsURL is the address of the Documents FOLDER in your app. It is not a FILE that you can get the contents of:

/var/mobile/Containers/Data/Application/7FA4D6A9-2149-4053-BF08-22E94A00AE34/Documents

like image 21
Matt_S Avatar answered Sep 28 '22 09:09

Matt_S