How to save a new file to iCloud Drive?

I have an iOS app and I need to be able to pick a file on my iCloud Drive, modify the file, and save the modified file with a new extension. I've tried lots of things but I still can't write the new file. Here is my latest code:


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)
       }
  }


When I pick a file on my iCloud Drive I get the following error:


You don’t have permission to save the file “TestFile.txt.copy” in the folder “Test Files”.


How can a save a modifed file?