ios - 是否有必要使用 URL bookmarkData 在 iOS 中持久化文件 URL?
<p><p>我正在 iOS 应用程序中编写持久层,遇到了 URL 的 <a href="https://developer.apple.com/documentation/foundation/url/2143023-bookmarkdata" rel="noreferrer noopener nofollow">bookmarkData</a>功能。它的用法看起来很简单,但是当我搜索这个主题的讨论时,我只看到在 macOS 应用程序中使用的书签数据。 </p>
<p>有问题的文件是由应用程序创建的,而不是由用户直接创建的,如果用户将它们从其目录中移出,那么应用程序可能会忘记该文件。 </p>
<p>考虑到这些细节,我应该保留书签数据以便在应用启动之间访问文件 URL,还是直接保存文件 URL 是否安全?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果文件在您应用的沙箱之外,您必须存储书签数据,您不能重用 UIDocumentPickerVC 提供的安全范围 URL,根据 <a href="https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller" rel="noreferrer noopener nofollow">documentation</a> .要获取此书签数据,您必须使用文件呈现器/协调器或 UIDocument 访问该文件。代码可能如下所示:</p>
<pre><code>func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: ) {
for url in urls {
//My experience is that your UIDocument subclass needs to be called first
//otherwise bookmarkData() will throw
//Instead of UIDocument you could call your own file presenter logic
var doc = MyUIDocument(fileURL: url)
let bookmarkData = try url.bookmarkData()
//Store your bookmarkData so you can later resolve a new URL
}
</code></pre>
<p>我希望这会有所帮助,因为当我尝试保存书签数据而不先处理文件时,我因错误而浪费了时间。</p></p>
<p style="font-size: 20px;">关于ios - 是否有必要使用 URL bookmarkData 在 iOS 中持久化文件 URL?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47166147/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47166147/
</a>
</p>
页:
[1]