1

私は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 を使用したくないのですが、それが唯一のオプションである場合はお知らせください。

4

1 に答える 1