4

I'm trying to rename a file (a FileWrapper containing a UIDocument subclass) as follows:

coordinationQueue.async {
    var error: NSError? = nil
    let fileCoordinator = NSFileCoordinator(filePresenter: self)
    fileCoordinator.coordinate(readingItemAt: url, options: .withoutChanges, 
      writingItemAt: targetURL, options: .forMoving, error: &error) { fromURL, toURL in
        do {
            fileCoordinator.item(at: fromURL, willMoveTo: toURL)
            try FileManager().moveItem(at: fromURL, to: toURL)
            fileCoordinator.item(at: fromURL, didMoveTo: toURL)

            // Success!
        } catch {
            // Handle error
        }
    }
    if let error = error {
        // Handle error
    }
}

… but it's failing on the moveItem, with the following error:

Error Domain=NSCocoaErrorDomain Code=513 "“from_file” couldn’t be moved because you don’t have permission to access “My_folder”." UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/My_folder/from_file, NSUserStringVariant=( Move ), NSDestinationFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/My_folder/to_file, NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/My_folder/from_file, NSUnderlyingError=0x1c1240ab0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

Interestingly, it works on fine on the simulator.

Can anybody help me out with this? (x-posted on Apple forums)

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • 1
    I think it works fine on simulator, because the path is on your local machine where you have definitely access to it. – Retterdesdialogs Sep 26 '17 at 16:18
  • A couple of thoughts - have you restarted the device (I've had numerous problems with needing to restart for coordinated files in iCloud, seems like an Apple bug). Also, does it work if you remove the surrounding item.(at:) calls? – Jordan Smith Sep 28 '17 at 01:09

0 Answers0