9

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lawgrin Foul
  • 308
  • 2
  • 10

4 Answers4

4

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.

Dan G
  • 232
  • 1
  • 8
  • Do you happen to know why this is happening? Also, how do obtain each URL? Is it from a AVAsset? – Shade Oct 18 '16 at 08:13
  • 2
    I've had a similar problem. I get a file system URL via `PHImageManager.defaultManager().requestAVAssetForVideo()`. If I attempt to use that URL more than once from `NSFileManager`, I get an error the second time. In my case, I have a workaround: I'm copying the file, and then doing some other things with it. If I access my new copy (rather than the original) for the second `NSFileManager` operation, it works. Must be a security thing. Weird... – Anna Dickinson Feb 09 '17 at 20:31
1

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.

MirekE
  • 11,515
  • 5
  • 35
  • 28
0

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.

zx485
  • 28,498
  • 28
  • 50
  • 59
leon wu
  • 315
  • 3
  • 10
-2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267