mywac Milan
mywac Milan

Reputation: 31

Writing file to iCloud drive Error Domain=NSCocoaErrorDomain Code=256

I am trying to send a file from local storage of sandboxed app to icloud drive. Unfortunately I am getting this error:

Error Domain=NSCocoaErrorDomain Code=256 "Soubor „About.txt" couldn't open." UserInfo={NSURL=file:///var/mobile/Containers/Data/Application/1626D575-64CF-4B61-B6B1-38F0B76ED135/Documents/path/path/About.txt, NSUserStringVariant=( "Cannot disable syncing on a unsynced item." ), NSUnderlyingError=0x13d819630 {Error Domain=NSPOSIXErrorDomain Code=37 "Operation already in progress"}}

My code is as follows:

struct DocumentsDirectory {
    static let localDocumentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    static let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
}

Copying function:

func copyFileFromLocacPathToIcloud (fileName:String, filePath:URL, folderName:String) {
   let fileManager = FileManager.default
   if (ICloudUtils.isiCloudEnabled(icloudURL: DocumentsDirectory.iCloudDocumentsURL)) {
      let fileUrl = DocumentsDirectory.localDocumentsURL!.appendingPathComponent("path", isDirectory: true).appendingPathComponent("path", isDirectory:true).appendingPathComponent("About.txt")
      if fileUrl.startAccessingSecurityScopedResource() {

      }
      Log.dbg(msg: "file exists at location \(fileManager.fileExists(atPath: fileUrl.path)) \(fileUrl)")
      let iCLoudURL = DocumentsDirectory.iCloudDocumentsURL?.appendingPathComponent(fileName)
      do {
         try fileManager.setUbiquitous(false, itemAt: fileUrl, destinationURL: iCLoudURL!)
      }catch {
        Log.error(msg: "icloud save file \(error)")
        fileUrl.stopAccessingSecurityScopedResource()

    }
  }
}

capabilities I have iCloud on. Somebody can help me with this issue ?

Upvotes: 0

Views: 495

Answers (1)

mestor
mestor

Reputation: 63

I know this question is old but shows up in google search so it may help others. According to this doc when you try to send the file to iCloud you should set the flag to true. you where using false which is to remove the file from iCloud. Basically change the line from:

try fileManager.setUbiquitous(false, itemAt: fileUrl, destinationURL: iCLoudURL!)

to:

try fileManager.setUbiquitous(true, itemAt: fileUrl, destinationURL: iCLoudURL!)

Hope that helps.

Upvotes: 2

Related Questions