2012-11-08 117 views
1

我是一个初学者使用Objective-C。我用下面的代码,将文件移动到iCloud,但它给出了The operation could not be completed. The file exists.移动/复制文件到iCloud

//store the file locally in document folder 
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [docPaths objectAtIndex:0]; 
filePath = [filePath stringByAppendingString:@"/"]; 
filePath = [filePath stringByAppendingString:fileName]; 

NSString *writeError = nil; 
NSData * fileData = [NSPropertyListSerialization dataFromPropertyList:dataDic format:NSPropertyListXMLFormat_v1_0 errorDescription:&writeError]; 

if ([fileData writeToFile:filePath atomically:YES]) { 
    NSLog(@"Server file is stored locally"); 
}else { 
    NSLog(@"%@", writeError); 
} 


// store the file in iCloud folder 

NSURL *ubiquitousURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; 
NSString *tmpubiquitousURL = ubiquitousURL.absoluteString; 
tmpubiquitousURL = [tmpubiquitousURL stringByAppendingString:fileName]; 
NSURL *ubi2 = [[NSURL alloc] initWithString:tmpubiquitousURL]; 

[[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:filePathURL destinationURL:ubi2 error:&error]; 

我用下面的删除从iCloud中的文件,但它给出了一个错误的错误Cannot disable syncing on an un-synced file.

[[NSFileManager defaultManager] setUbiquitous:NO itemAtURL:filePathURL destinationURL:ubi2 error:&error]; 

我在我的应用代理中检查了iCloud的可用性,并且它是可用的。该文件是一个XML文件(.plist),我有一个本地副本存储在NSDocumentDirectory。总体而言,我想在iCloud中同步该文件,以便使用我的应用在所有设备上都可以访问该文件。我一直在为此挣扎2天,所以如果你能帮我解决这个问题,我将不胜感激。

注意:我宁愿不使用UIDocument,但是,如果这是唯一的选择,请让我知道。

回答

0

我也有同样的问题,同时使用的代码

[[NSFileManager defaultManager] setUbiquitous:NO itemAtURL:filePathURL destinationURL:ubi2 error:&error]; 

你必须改变如下面的代码,使其正常工作

[[[NSFileManager alloc]init]setUbiquitous:YES itemAtURL:filePathURL destinationURL:ubi2 error:nil]; 

这个代码是用于移动文件到icloud ,你也应该改变你正在移动的文件的名称。它不应该是一样的。

相关问题