且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSFileManager moveItem引发错误代码= 513

更新时间:2023-11-15 20:02:58

我解决了我的问题,但这可能与您遇到的情况有所不同:更改为copyItem时,我仍然遇到问题.我在这里添加我的发现作为答案,以防其他人像我一样偶然发现此问题,并且可以帮助他们解决问题.

I solved my issue, but it may be different to what you were experiencing: I still had issues when changing to copyItem. I'm adding my findings here as an answer in case anyone else stumbles upon this question as I did and it helps them out.

我的应用程序加载可以从我的网站下载的自定义文件.使用iOS 13的下载管理器,这些文件将首先复制到下载"文件夹,然后可以由我的应用程序打开.但是,当我尝试以与iOS 12相同的方式访问这些文件时,我的应用出现权限错误.

My app loads custom files that can be downloaded from my website. With iOS 13's download manager, these are copied to the Downloads folder first, and can then be opened by my app. Yet my app was having permission errors when attempting to access these files in the same way as iOS 12.

对另一个问题的答案提示了我.我需要按以下方式调用startAccessingSecurityScopedResource()stopAccessingSecurityScopedResource() :

This answer to a different question clued me in. I need to call startAccessingSecurityScopedResource() and stopAccessingSecurityScopedResource() as follows:

// source and destination are URLs
if source.startAccessingSecurityScopedResource() {
    try FileManager.default.moveItem(at: source, to: destination)
}
source.stopAccessingSecurityScopedResource()

Apple文档并不真正表明这是是iOS 13的新功能,但我认为文件下载的方法不同,这意味着需要考虑沙箱操作.

The Apple documentation doesn't really suggest that this is new with iOS 13 but I think just the different approach to file downloading means sandboxing considerations need to be made.