Rename an iCloud document

Problem

What I still have is code that creates a new local file and deletes the iCloud file.

Can I rename an iCloud document so that it stays in iCloud?

GarageBand can do this. You can rename the song iCloud. After renaming, the song is still in iCloud. However, GarageBand is an Apple app, so it can use private apis.

My current code is:

- (void)moveFrom:(NSURL*)sourceURL moveTo:(NSString*)destinationName completion:(void (^)())completion { MyDocument *document = [[MyDocument alloc] initWithFileURL:sourceURL]; [document openWithCompletionHandler:^(BOOL success) { NSURL *fileURL = [self.localRoot URLByAppendingPathComponent:destinationName]; DLog(@"Create %@", fileURL); [document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { NSLog(@"Saved %@", fileURL); [document closeWithCompletionHandler:^(BOOL success) { // Delete the old document from a secondary thread dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^() { NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; [fileCoordinator coordinateWritingItemAtURL:sourceURL options:NSFileCoordinatorWritingForDeleting error:nil byAccessor:^(NSURL* writingURL) { NSFileManager* fileManager = [[NSFileManager alloc] init]; [fileManager removeItemAtURL:writingURL error:nil]; DLog(@"Deleted %@", sourceURL); completion(); }]; }); }]; }]; }]; } 

Update: not lucky yet

I found that -setUbiquitous:itemAtURL:destinationURL:error: cannot be used to rename documents.

If I call [setUbiquitous: NO itemAtURL: oldLocalURL destinationURL: newLocalURL error: & error] in an already local file, then:

Domain Error = NSCocoaErrorDomain Code = 512 "Operation could not be completed. (Cocoa error 512.)" UserInfo = 0x1fdf6730 {NSURL = File: // localhost / var / mobile / Applications / 4BABA000-B100-49FC-B928-B0F403FC75FF / Documents /LocalDrawing.td2/, NSUnderlyingError = 0x20940e80 "Operation could not be completed. (Error LibrarianErrorDomain 2 - Unable to disable synchronization on an unsynchronized item.)"}

If I call [setUbiquitous: YES itemAtURL: oldCloudURL destinationURL: newCloudURL error: & error] in an already cloud file, then:

Domain Error = NSCocoaErrorDomain Code = 512 "Operation could not be completed. (Cocoa error 512.)" UserInfo = 0x208e9820 {NSURL = File: // localhost / var / mobile / Library / Mobile% 20Documents / 22DR89XVRF ~ com ~ opcoders ~ triangle -draw / Documents / CloudDrawing.td2 /, NSUnderlyingError = 0x208d45b0 "The operation could not be completed. (Error LibrarianErrorDomain 2 - Unable to enable synchronization of the synchronized item.)"}

Thus, -setUbiquitous:itemAtURL:destinationURL:error: cannot be used to rename documents.

+4
source share
2 answers

I finally decided. This is my code:

 - (void)_moveURL:(NSURL*)sourceURL destURL:(NSURL*)destinationURL success:(void (^)())successBlock failure:(void (^)(NSError *))failureBlock { NSParameterAssert(sourceURL); NSParameterAssert(destinationURL); NSParameterAssert(successBlock); NSParameterAssert(failureBlock); // Do the actual renaming __block NSError *moveError = nil; __block BOOL moveSuccess = NO; void (^accessor)(NSURL*, NSURL*) = ^(NSURL *newURL1, NSURL *newURL2) { NSFileManager *fileManager = [[NSFileManager alloc] init]; moveSuccess = [fileManager moveItemAtURL:sourceURL toURL:destinationURL error:&moveError]; }; // Coordinate renaming NSError *coordinatorError = nil; NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; [coordinator coordinateWritingItemAtURL:sourceURL options:NSFileCoordinatorWritingForMoving writingItemAtURL:destinationURL options:NSFileCoordinatorWritingForReplacing error:&coordinatorError byAccessor:accessor]; if (moveSuccess) { successBlock(); return; } if (moveError) { failureBlock(moveError); return; } if (coordinatorError) { failureBlock(coordinatorError); return; } NSAssert(NO, @"should not happen"); } 
+7
source

There is an easier way to rename an item at any given URL, including iCloud URLs.

 url.setResourceValue(newName, forKey: NSURLNameKey) 

I use this in my cocoa application code.

0
source

All Articles