【问题标题】:NSFileManager moveItem throws Error Code=513NSFileManager moveItem 抛出错误代码=513
【发布时间】:2019-12-28 14:07:55
【问题描述】:

我正在尝试将一个文件从一个 URL 移动到另一个,但是我收到代码 513 的错误。我知道这是NSFileWriteNoPermissionError。考虑到我首先创建了文件夹,我看不出这怎么可能。

//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 Code=513 ""trim.90A10B33-B884-419F-BAD9-65583531C3C3.MOV" 无法移动,因为您没有访问“AV”的权限。" UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/PluginKitPlugin/0849234B-837C-43ED-BEDD-DE4F79E7CE96/tmp/trim.90A10B33-B884-419F-BAD9-65583531C3C3.MOV, NSUserStringVariant=( 移动

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

P.S:这只发生在 iOS 13 上。

【问题讨论】:

  • 你怎么知道你有权访问源 URL?
  • @ElTomato 我已经用错误更新了问题。
  • 我遇到了同样的问题。你解决了吗?
  • 作为临时解决方法,我使用copy 方法而不是move,它可以工作。
  • 我遇到了这个问题,没有任何帮助。还有其他解决方案吗?

标签: ios swift xcode nsfilemanager ios13


【解决方案1】:

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

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

This answer to a different question 提示我。我需要拨打startAccessingSecurityScopedResource()stopAccessingSecurityScopedResource() 如下:

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

Apple documentation 并不是真的表明这是 iOS 13 的新功能,但我认为只是文件下载的不同方法意味着需要考虑沙盒。

【讨论】:

  • 这为我解决了,虽然 stopAccessingSecurityScopedResource() 调用应该在括号内。
  • 在我的情况下不起作用。我尝试在源和目标上调用startAccessingSecurityScopedResource(),但在这两种情况下都返回false。将其从 moveItem 更改为 copyItem 是我的解决方法(从 UIImagePickerController 选择中移动视频)
猜你喜欢
  • 2013-09-20
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多