问题所在
到目前为止,我拥有的是创建一个新的本地文件并删除iCloud文件的代码。
是否可以重命名iCloud文档,使其保持在iCloud?中
GarageBand可以做到。可以重命名一首iCloud歌曲。重命名完成后,歌曲仍在iCloud中。然而,GarageBand是一个苹果应用程序,所以它可以使用私有apis。
我现在的代码是:
- (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();
}];
});
}];
}];
}];
}更新:仍然没有运气
我发现-setUbiquitous:itemAtURL:destinationURL:error:不能用于重命名文档。
如果我在已经本地的文件上调用setUbiquitous:NO itemAtURL:oldLocalURL destinationURL:newLocalURL错误:&error,那么:
错误Domain=NSCocoaErrorDomain Code=512“操作无法完成。(可可错误512。){NSURL=file://localhost/var/mobile/Applications/4BABA000-B100-49FC-B928-B0F403FC75FF/Documents/LocalDrawing.td2/,UserInfo=0x1fdf6730 NSUnderlyingError=0x20940e80“操作无法完成。(LibrarianErrorDomain错误2-无法禁用对未同步项的同步。)”}
如果我调用setUbiquitous:YES itemAtURL:oldCloudURL destinationURL:newCloudURL错误:&error,那么:
错误Domain=NSCocoaErrorDomain Code=512“操作无法完成。(可可错误512。){NSURL=file://localhost/var/mobile/Library/Mobile%20Documents/22DR89XVRF~com~opcoders~triangle-draw/Documents/CloudDrawing.td2/,UserInfo=0x208e9820 NSUnderlyingError=0x208d45b0“操作无法完成。(LibrarianErrorDomain错误2-无法在同步项上启用同步。)”}
因此,-setUbiquitous:itemAtURL:destinationURL:error:不能用于重命名文档。
发布于 2013-03-31 01:22:58
我终于解决了。到目前为止,这是我的代码:
- (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");
}发布于 2016-03-17 00:17:01
有一种更简单的方法可以在任何给定的网址上重命名项目,包括iCloud URL。
url.setResourceValue(newName, forKey: NSURLNameKey)我正在我的可可应用程序的生产代码中使用这个。
编辑: Swift 5中的,这看起来有点不同,但仍然运行良好:
var resourceValues = URLResourceValues()
resourceValues.name = newName
try previousFileURL.setResourceValues(resourceValues)https://stackoverflow.com/questions/14358504
复制相似问题