在iOS 13中从外部存储读取文件 [英] Reading files from external storage in iOS 13

查看:1027
本文介绍了在iOS 13中从外部存储读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用,该应用正在尝试从外部存储设备读取文件,而不将其导入到应用的沙箱中.

I have an iOS app that is trying to read files from an external storage device without importing them into the App's sandbox.

我已按照此处概述的Apple文档进行操作-

I have followed Apple's documentations outlined here to do this --

提供对目录的访问权限

我能够检索所选目录(位于通过Lightning端口连接的外部存储设备上)并枚举目录中的文件.

I'm able to retrieve the selected directory ( which is on an external storage device connected via the Lightning port ) and enumerate the files inside the directory.

但是,当我尝试按照推荐的方式对那些文件进行操作时,我失败了,并且基本上得到了文件上的权限错误.

However, when I try to do something with those files as per the recommended pattern, I get a failure and basically get permission errors on the file.

        let shouldStopAccessing = pickedFolderURL.startAccessingSecurityScopedResource()
        defer {
          if shouldStopAccessing {
            pickedFolderURL.stopAccessingSecurityScopedResource()
          }
       }
       var coordinatedError:NSError?
       NSFileCoordinator().coordinate(readingItemAt: pickedFolderURL, error: &coordinatedError) { (folderURL) in
        let keys : [URLResourceKey] = [.isDirectoryKey]
        let fileList = FileManager.default.enumerator(at: pickedFolderURL, includingPropertiesForKeys: keys)!
        for case let file as URL in fileList {
            if !file.hasDirectoryPath {
                do {
                    // Start accessing a security-scoped resource.
                    guard file.startAccessingSecurityScopedResource() else {
                        // Handle the failure here.
                        //THIS ALWAYS FAILS!!
                        return
                    }

                    // Make sure you release the security-scoped resource when you are done.
                    defer { file.stopAccessingSecurityScopedResource() }

我应该补充一点,如果文件通过模拟器位于iCloud Drive上,则可以正常工作.在外部设备和真实设备上的iCloud Drive上都会失败.

I should add that this works JUST FINE if the files are on iCloud Drive via Simulator. It fails both on external devices and iCloud Drive on a real device.

此处是一个完整的工作项目,演示了失败.

Here is a full working project that demonstrates the failure.

  1. 在模拟器上运行可以很好地访问iCloud Drive文件.但是在设备上运行失败.
  2. 在设备上运行以访问USB驱动器失败.

推荐答案

因此,这似乎是文档问题,上面有链接.当用户选择一个文件夹时,所有文件和文件夹都将被递归授予访问权限,并自动在安全范围内. guard file.startAccessingSecurityScopedResource()行始终返回false.

So, this seems like a documentation issue with the link posted above. When the user selects a folder, all files and folders are recursively granted access and automatically security scoped. The line guard file.startAccessingSecurityScopedResource() always returns false.

完成这项工作的诀窍不是尝试对单个文件进行安全作用域设置,而是确保在访问文件之前此代码段不运行.

The trick to getting this work is NOT to try to security scope individual files, but to ensure that this code snippet does not run BEFORE you access files.

   defer {
      if shouldStopAccessing {
        pickedFolderURL.stopAccessingSecurityScopedResource()
      }
   }

只要您在pickedFolderURL处于安全范围内时继续访问文件,就可以成功.

As long as you continue to access files while the pickedFolderURL is inside security scope, you will be successful.

希望这对某人有帮助.

Hope this helps somebody.

这篇关于在iOS 13中从外部存储读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆