【问题标题】:how can I read a file out of sandbox? (swift 5 , xcode 12)如何从沙盒中读取文件? (快速 5,xcode 12)
【发布时间】:2021-09-27 22:53:35
【问题描述】:

我需要向服务器发送一个文件。我使用提供文件 url 的 UIViewControllerRepresentable 选择此文件,每次尝试上传时都会收到相同的消息:- Domain=NSCocoaErrorDomain Code=257 “无法打开文件“example1.xml”,因为您没有无权查看。” .我想我需要在 info.plist 上添加权限,但我找不到合适的权限。如果我在沙箱中选择一个文件,它就可以工作。

【问题讨论】:

    标签: swift5 sandbox ios14 info.plist security-scoped-bookmarks


    【解决方案1】:

    我通过在 URL 上使用 .startAccessingSecurityScopedResource () 解决了这个问题。例如:

    `var path = URL(字符串:“pathFile”)!

    //读取文件之前

    path.startAccessingSecurityScopedResource()

    //最后

    path.stopAccessingSecurityScopedResource()

    //关闭。 `

    资源https://developer.apple.com/documentation/foundation/nsurl/1417051-startaccessingsecurityscopedreso

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多