Kun*_*hah 6 xcode nsfilemanager ios swift ios13
我正在尝试将文件从URL移到另一个文件,但是我收到code错误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)")
}
Run Code Online (Sandbox Code Playgroud)
错误
“无法移动错误域= NSCocoaErrorDomain代码= 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 =(Move
我尝试将最初创建的文件夹的权限更改为0o777,但是当我打印其属性时,它会返回511作为其权限。
PS:这仅在iOS 13上发生。
我解决了我的问题,但可能与您遇到的情况不同:更改为copyItem. 我在这里添加我的发现作为答案,以防其他人像我一样偶然发现这个问题,这对他们有帮助。
我的应用程序加载可以从我的网站下载的自定义文件。使用 iOS 13 的下载管理器,这些文件首先被复制到“下载”文件夹,然后可以由我的应用程序打开。然而,当我的应用程序尝试以与 iOS 12 相同的方式访问这些文件时,出现权限错误。
对另一个问题的回答让我明白了。我需要打电话startAccessingSecurityScopedResource(),stopAccessingSecurityScopedResource()如下:
// source and destination are URLs
if source.startAccessingSecurityScopedResource() {
try FileManager.default.moveItem(at: source, to: destination)
}
source.stopAccessingSecurityScopedResource()
Run Code Online (Sandbox Code Playgroud)
苹果文档并没有真正表明这是 iOS 13 的新功能,但我认为只是文件下载的不同方法意味着需要考虑沙箱。
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |