【问题标题】:Some files in Documents subfolders cannot be access while device is locked / screen is off设备锁定/屏幕关闭时无法访问 Documents 子文件夹中的某些文件
【发布时间】:2019-11-21 19:49:21
【问题描述】:

在设备锁定时无法访问 Documents 子文件夹中的某些文件。

AVAudioFile(forReading: currentPlaylistURLs[index!])

当设备被锁定或屏幕关闭并且 URL 指向 Documents 文件夹中某个子文件夹中的某个文件时,上述行将引发错误。并非所有子文件夹都会导致错误(为什么??)。当设备解锁时没有错误。我在 iOS 13.2.3 中注意到了这种行为,因此在此版本之前它运行良好。

错误是:

[AVAudioFile.mm:134:AVAudioFileImpl: (ExtAudioFileOpenURL((CFURLRef)fileURL, &_extAudioFile)): error -54
Error Domain=com.apple.coreaudio.avfaudio Code=-54 "(null)" UserInfo={failed call=ExtAudioFileOpenURL((CFURLRef)fileURL, &_extAudioFile)}

有人经历过吗?

【问题讨论】:

  • 尝试在没有保护的情况下写入文件write(to: fileURL, options: .noProtection)developer.apple.com/documentation/uikit/…
  • @LeoDabus App 不写入文件。文件通过空投传输,使用 Files 应用程序打开,然后保存到应用程序的 Documents 子文件夹中。子文件夹是在保存时从“文件”应用创建的。
  • 要更改现有文件的数据保护级别,请使用 NSURL 的 setResourceValue(_:forKey:) 方法。调用此方法时,将新的数据保护选项分配给 fileProtectionKey 资源键。清单 2 显示了一个将该键添加到现有文件的示例。
  • @LeoDabus 根据您的建议,我在初始化 AVAudioFile 之前将其设置为 .none 但我仍然在同一行收到相同的错误。
  • @LeoDabus 更改文件权限似乎是解决方法的正确解决方案。奇怪的是,在从中实例化 AVAudioFile 之前设置文件权限还不够快。每当它获得新列表时循环遍历列表似乎已经修复了这个错误。当有新文件添加到列表中时,我还必须为单个文件设置权限。如果你愿意,你可以添加一个答案,我会标记它。我将尝试设置目录权限,而不是遍历所有文件。感谢您的帮助!

标签: swift core-audio ios13


【解决方案1】:

您需要将文件的文件保护密钥权限设置为.none 及其父文件夹:

extension URL {
    var parentDirectory: URL? { try? resourceValues(forKeys: [.parentDirectoryURLKey]).parentDirectory }
    var fileProtection: URLFileProtection? { try? resourceValues(forKeys: [.fileProtectionKey]).fileProtection }
    func disableFileProtection() throws { try (self as NSURL).setResourceValue(URLFileProtection.none, forKey: .fileProtectionKey) }
}

let fileURL = URL.init(...)
if let parentDirectory = fileURL.parentDirectory {
    do {
        try parentDirectory.disableFileProtection()
        try fileURL.disableFileProtection()
    }
    catch { print(error) }
}

【讨论】:

    猜你喜欢
    • 2016-07-03
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2014-09-26
    • 2012-05-26
    相关资源
    最近更新 更多