Lawgrin Foul
Lawgrin Foul

Reputation: 308

iOS 9 read file permission

In iOS 9+ I get a nil on any attempt to read from file. The file in this case is a image file path.
using

NSData(contentsOfFile: stringpath, options: NSDataReadingOptions.DataReadingUncached)

or

NSData(contentsOfFile: stringpath)

Actions:
I have removed the "file://" from the path and it now has a permissions issue.

Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0048.JPG” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG, NSUnderlyingError=0x13f978f50 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

I have added the NSAllowArbitraryLoads and set it to true.
I have tried to look for the file myself using "NSSearchPathDirectory" however the paths do not match in any way

Upvotes: 9

Views: 6361

Answers (4)

Cœur
Cœur

Reputation: 38737

In my case, the file permissions were too restrictive, so I couldn't read the file.

Adding read+write permissions to the file before accessing it solved it.

do {
    // Retrieve any existing attributes
    var attrs = try FileManager.default.attributesOfItem(atPath: stringpath)
    let existing = (attrs as NSDictionary).filePosixPermissions()
    // Set the read+write value in the attributes dict
    attrs[.posixPermissions] = existing | 0b110000000
    // Update attributes
    try FileManager.default.setAttributes(attrs, ofItemAtPath: stringpath)

    // Read data from file
    let data = try Data(contentsOf: URL(fileURLWithPath: stringpath, isDirectory: false), options: .uncached)
    print("success: \(data.count)")
} catch {
    print(error)
}

That works if you're in a folder with enough permissions, as you can change the files permissions even if you didn't had read permission on the file previously. This solution was applied at https://github.com/ZipArchive/ZipArchive/issues/293.

Upvotes: -2

leon wu
leon wu

Reputation: 315

In your Application, you don't have permission to access the file of /var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG, because of Sandboxing.

So, whatever you do, you can't initialize NSData or UIImage with the file path. But you can access the file of /var/mobile/Media/DCIM/100APPLE/xxx.mov with AVURLAsset. In my application, I extracted the data rather than URL from gallery by Photos kit and initialized UIImage with the data.

PHImageManager.default().requestImageData(
    for: assetObject!, options: options,
    resultHandler: {
        data, _, _, _ in
        if data != nil {
            self.assetUrl = movieMaker.createMovieFrom(imageData: data!, duration: Int(CXPreparetValue.imageDuration))
        }
})

it works for me! If you have other opinions, please tell me.

Upvotes: 0

Dan G
Dan G

Reputation: 232

I encountered this error because I was attempting to access multiple files in the same block. The fix that worked for me was changing the code structure such that each file url was obtained, then read from, before attempting to get the next file url.

Upvotes: 4

MirekE
MirekE

Reputation: 11555

You are most likely getting this error, because iOS apps only have access to files within its sandbox. See Apple documentation on file systems for details.

Upvotes: 1

Related Questions