[英]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.