iOS 9 read file permission

好久不见. 提交于 2019-11-28 07:25:19

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/33478142/ios-9-read-file-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!