1

我需要在其中创建文件夹Cloud并将我的.txt文本文件保存到该文件夹​​中。

所以我用以下代码创建了文件夹AppDelegate

NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    NSError *error = nil;

    if(ubiq)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        NSURL *rootURL = [fm URLForUbiquityContainerIdentifier:nil];
        NSURL *newFolder = [NSURL URLWithString:@"Notes" relativeToURL:rootURL];

        [fm createDirectoryAtURL:newFolder withIntermediateDirectories:YES attributes:nil error:&error];

        if(error)
        {
            NSLog(@"Error");
        }

        else
        {
            NSLog(@"NO Error");
        }
    }

我测试后,它显示没有错误。所以我假设它在 iCloud 中创建了 Notes 文件夹。

但是,当我使用以下代码将文本文档保存到该文件夹​​中时,它显示错误并且不保存。

NSString *file = [NSString stringWithFormat:@"%@.txt",fileName];

    NSURL *ubiq = [[NSFileManager defaultManager]
                   URLForUbiquityContainerIdentifier:nil];
    NSURL *ubiquitousPackage =
    [[[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"Notes"]
     URLByAppendingPathComponent:file];

    Note *doc = [[Note alloc] initWithFileURL:ubiquitousPackage];

    [doc saveToURL:[doc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        if (!success)
        {
            NSLog(@"Error");
        }

        else
        {
            doc.noteContent = self.txtView.text;
            [doc updateChangeCount:UIDocumentChangeDone];
        }
    }];

错误信息是

Foundation called mkdir("/private/var/mobile/Library/Mobile Documents/35QNLTQU29~com~myapp~iCloudTest/Documents/Notes/(A Document Being Saved By iCloudTest)"), it didn't return 0, and errno was set to 2.

我该如何解决?

4

0 回答 0