简体   繁体   English

iOS 9读取文件权限

[英]iOS 9 read file permission

In iOS 9+ I get a nil on any attempt to read from file. 在iOS 9+中,任何从文件读取的尝试均无效。 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. 我已经从路径中删除了“ file://”,并且现在存在权限问题。

Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0048.JPG” couldn't be opened because you don't have permission to view it." 错误域= NSCocoaErrorDomain代码= 257“无法打开文件“ IMG_0048.JPG”,因为您没有查看权限。” UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG, NSUnderlyingError=0x13f978f50 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} UserInfo = {NSFilePath = / var / mobile / Media / DCIM / 100APPLE / IMG_0048.JPG,NSUnderlyingError = 0x13f978f50 {Error Domain = NSPOSIXErrorDomain Code = 1“不允许操作”}}

I have added the NSAllowArbitraryLoads and set it to true. 我添加了NSAllowArbitraryLoads并将其设置为true。
I have tried to look for the file myself using " NSSearchPathDirectory " however the paths do not match in any way 我试图使用“ NSSearchPathDirectory ”自己查找文件,但是路径完全不匹配

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. 对我有用的解决方案是更改代码结构,以便在尝试获取下一个文件url之前先获取每个文件的url,然后从中读取。

You are most likely getting this error, because iOS apps only have access to files within its sandbox. 您很可能会收到此错误,因为iOS应用程序只能访问其沙箱中的文件。 See Apple documentation on file systems for details. 有关详细信息,请参阅文件系统上的Apple 文档

In your Application, you don't have permission to access the file of /var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG , because of Sandboxing. 在您的应用程序中,由于沙箱操作,您无权访问/var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG文件。

So, whatever you do, you can't initialize NSData or UIImage with the file path. 因此,无论您做什么,都无法使用文件路径初始化NSDataUIImage But you can access the file of /var/mobile/Media/DCIM/100APPLE/xxx.mov with AVURLAsset . 但是,你可以访问的文件/var/mobile/Media/DCIM/100APPLE/xxx.movAVURLAsset In my application, I extracted the data rather than URL from gallery by Photos kit and initialized UIImage with the data. 在我的应用程序中,我通过“照片”工具包从库中提取了数据而不是URL,并使用数据初始化了UIImage

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. 如果您有其他意见,请告诉我。

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://github.com/ZipArchive/ZipArchive/issues/293中应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM