File rename failing with NSCocoaErrorDomain Code=513 on device

898 Views Asked by At

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)

0

There are 0 best solutions below