[英]Save file using Swift and iOS
我有一个 iOS 应用程序,我需要能够在我的 iCloud Drive 上选择一个文件,修改该文件,并使用新的扩展名保存修改后的文件。 我已经尝试了很多东西,但我仍然无法编写新文件。 这是我的最新代码:
documentPickerViewController = DocumentPickerViewController(documentTypes: ["public.item"], in: .open)
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let fileURL = urls[0]
do {
// Read the file
_ = fileURL.startAccessingSecurityScopedResource()
let fileData = try Data(contentsOf: fileURL, options: .uncached)
fileURL.stopAccessingSecurityScopedResource()
// write the file
let fileCopyURL = fileURL.appendingPathExtension("copy")
_ = fileCopyURL.startAccessingSecurityScopedResource()
try fileData.write(to: fileCopyURL)
fileCopyURL.stopAccessingSecurityScopedResource()
}
catch {
print(error.localizedDescription)
}
}
当我在 iCloud Drive 上选择文件时,出现以下错误:
您无权将“TestFile.txt.copy”文件保存在“Test Files”文件夹中。
如何保存修改后的文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.