首页 > 解决方案 > 如何从沙盒中读取文件?(快速 5,xcode 12)

问题描述

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

标签: swift5sandboxios14info.plistsecurity-scoped-bookmarks

解决方案


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

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

//读取文件之前

path.startAccessingSecurityScopedResource()

//在最后

path.stopAccessingSecurityScopedResource()

//关闭。`

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


推荐阅读