Read permission for iOS 9 file

In iOS 9+, I get zero when I try to read from a file. The file in this case is the path to the image file.
using

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

or

 NSData(contentsOfFile: stringpath) 

Actions:
I deleted the "file: //" from the path, and now it has a permission problem.

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

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

+7
source share
4 answers

I ran into this error because I was trying to access multiple files in one block. The fix that worked for me was to change the structure of the code so that each file URL was received and then read before trying to get the next file URL.

+4
source

You will most likely get this error because iOS apps have access to files in their sandbox. See the Apple documentation on file systems for more details.

+1
source

In your application, you do not have permission to access the /var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG file due to the sandbox.

So, whatever you do, you cannot initialize NSData or UIImage using the file path. But you can access the /var/mobile/Media/DCIM/100APPLE/xxx.mov file using AVURLAsset . In my application, I extracted data, not the URL from the Photo Gallery, and initialized the UIImage data with data.

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

he works for me! If you have other opinions, let me know.

0
source

In my case, the file permissions were too strict, so I could not read the file.

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

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

This works if you are in a folder with sufficient permissions, since you can change the file permissions even if you did not have permission to read the file before. This solution was applied at https://github.com/ZipArchive/ZipArchive/issues/293 .

-2
source

All Articles