swift5 - 如何从沙盒中读取文件?(快速 5,xcode 12)
问题描述
我需要将文件发送到服务器。我使用提供文件 url 的 UIViewControllerRepresentable 选择此文件,每次尝试上传时都会收到相同的消息:- Domain=NSCocoaErrorDomain Code=257 “无法打开文件“example1.xml”,因为您没有无权查看。” . 我想我需要在 info.plist 上添加权限,但我找不到合适的权限。如果我在沙箱中选择一个文件,它就可以工作。
解决方案
我通过在 URL 上使用 .startAccessingSecurityScopedResource () 解决了这个问题。例如:
`var path = URL(字符串:“pathFile”)!
//读取文件之前
path.startAccessingSecurityScopedResource()
//在最后
path.stopAccessingSecurityScopedResource()
//关闭。`
资源https://developer.apple.com/documentation/foundation/nsurl/1417051-startaccessingsecurityscopedreso
推荐阅读
- java - Java中的贝塞尔曲线与点
- c# - C# WCF 客户端参数异常 XmlNode[] 无法转换为类型“...”
- ios - 如何在我的 ios 应用程序中检索嵌套的 firebase 数据?
- python - 如何合并 2 个不同长度的数据框并保留第一个数据框的所有位置?
- python - 无法导入 Selenium 包
- vb6 - 我如何以编程方式知道哪一个是物理 MAC 地址?
- javascript - 如何制作斯坦福专业页面中的动画?
- c# - 检查是否使用带有 Wix 的 CustomAction 安装了 .NETCore
- oracle - 错误 ORA-01722:创建实体化视图时编号无效(Oracle SDE 问题?)
- visual-studio-code - 有什么方法可以直观地更改 VS Code 中特定编辑器的表示?