【问题标题】:move/copy a file to iCloud将文件移动/复制到 iCloud
【发布时间】:2012-11-08 05:01:31
【问题描述】:

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

//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,但是,如果这是唯一的选择,请告诉我。

【问题讨论】:

    标签: objective-c icloud


    【解决方案1】:

    我在使用代码时也遇到了同样的问题

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

    您必须像下面这样更改代码才能正常工作

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

    此代码用于将文件移动到 icloud,您还应该更改要移动的文件的名称。应该不一样。

    【讨论】:

      猜你喜欢
      • 2014-05-22
      • 2013-12-30
      • 2011-12-24
      • 2015-11-24
      • 2014-08-15
      • 2013-12-24
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      相关资源
      最近更新 更多