我已经在这里看到这个问题了十几次,但是解决方案似乎无法解决我的情况。
我有一些内容需要存储在'Application Support'目录中,但是尝试在该目录中写入时继续收到错误。
func assetFilesDirectory(asset: Asset, shouldCreate: Bool) -> URL? {
do {
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let folder = applicationSupportFolderURL.appendingPathComponent("\(asset)")
if !FileManager.default.fileExists(atPath: folder.path) {
if shouldCreate {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
} else {
return nil
}
}
return folder
} catch {
print(error)
return nil
}
}
此代码运行时,我不断收到以下错误:
“您无权将文件“资产背景”保存在“应用程序支持”文件夹中。”
我已经尝试在下一行中切换create参数的值,但是我也遇到了同样的错误,只是引用了[[Application Support目录和Library。
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
我还没有看到任何理由使它在API方面有任何不同,但是作为信息,我正在tvOS平台(物理设备)上运行此代码。没有信息表明应该在tvOS上受到权限限制。Application Support
func assetFilesDirectory(name: String, shouldCreate: Bool) -> URL? {
do {
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let folder = applicationSupportFolderURL.appendingPathComponent(name)
if !FileManager.default.fileExists(atPath: folder.path) {
if shouldCreate {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
print("ok")
} else {
return nil
}
}
return folder
} catch {
print(error)
return nil
}
}
我打电话
let url = self.assetFilesDirectory(name: "test", shouldCreate: true) print(url)
我回到"ok"
和URLfile:///blahblahblah/Library/Application%20Support/test
,这正是人们所期望的。