NSFileManager moveItem引发错误代码= 513

问题描述:

我正在尝试将文件从URL移到另一个文件,但是代码为513时出现错误.我知道这是一个NSFileWriteNoPermissionError.考虑到我首先创建了文件夹,因此我看不出这是怎么回事.

I'm trying to move a file from a URL to another, however I get an error with code 513. I understand that this is a NSFileWriteNoPermissionError. I don't see how this possible considering I created the folder in the first place.

//self.video!.folderURL.path = /var/mobile/Containers/Data/Application/066BDB03-FD47-48D8-B6F8-932AFB174DF7/Documents/AV/769c504203024bae95b47d78d8fe9029
try? fileManager.createDirectory(atPath: self.video!.folderURL.path, withIntermediateDirectories: true, attributes: nil)

// Update permission for AV folder
do {
     let parentDirectoryPath = self.video!.folderURL.deletingLastPathComponent().path
     let result = try fileManager.setAttributes([FileAttributeKey.posixPermissions: 0o777], ofItemAtPath: parentDirectoryPath)
        print(result)
     } catch {
        print("Error = \(error)")
     }

// sourceURL = file:///private/var/mobile/Containers/Data/PluginKitPlugin/50D8B8DB-19D3-4DD6-93DD-55F37CF87EA7/tmp/trim.6D37DFD2-5F27-4AB9-B478-5FED8AA6ABD7.MOV
// self.url = file:///var/mobile/Containers/Data/Application/066BDB03-FD47-48D8-B6F8-932AFB174DF7/Documents/AV/0b0b6803e891780850152eeab450a2ae.mov
do {
     try fileManager.moveItem(at: sourceURL, to: self.url)
   } catch {
     QLogTools.logError("Error moving video clip file \(error)")
   }

错误

Error Domain = NSCocoaErrorDomain代码= 513由于您无权访问"AV",因此无法移动"trim.90A10B33-B884-419F-BAD9-65583531C3C3.MOV". UserInfo = {NSSourceFilePathErrorKey =/private/var/mobile/Containers/Data/PluginKitPlugin/0849234B-837C-43ED-BEDD-DE4F79E7CE96/tmp/trim.90A10B33-B884-419F-BAD9-65583531C3C3.MOV,NSUserStringVariant =( 移动

Error Domain=NSCocoaErrorDomain Code=513 ""trim.90A10B33-B884-419F-BAD9-65583531C3C3.MOV" couldn’t be moved because you don’t have permission to access "AV"." UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/PluginKitPlugin/0849234B-837C-43ED-BEDD-DE4F79E7CE96/tmp/trim.90A10B33-B884-419F-BAD9-65583531C3C3.MOV, NSUserStringVariant=( Move

我尝试将最初创建的文件夹的权限更改为0o777,但是当我打印其属性时,它的权限返回511.

I tried altering the permission of the folder I created in the first place to 0o777, but when I print out its attributes it returns 511 as its permissions.

PS :仅在iOS 13上发生.

P.S:This only happens on iOS 13.

我解决了我的问题,但这可能与您遇到的情况有所不同:更改为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.