1

我正在尝试集成 iCloud。

一切正常,但是当我尝试从 iCloud 读取文件时,我收到警告,例如:

Foundation called mkdir("/var/mobile/Library/Mobile Documents/.ubd/peer-E8A60A8F-FB9D-8721-F47C-hdffgdfg-v23/ftr/(A Document Being Saved By XYZ)"), it didn't return 0, and errno was set to 1.

我的代码来获取数据:

for (NSMetadataItem *item in results)
        {
            NSString *filename = [item valueForAttribute:NSMetadataItemDisplayNameKey];
            NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
            MyDocument *doc = [[MyDocument alloc] initWithFileURL:url];

                [doc openWithCompletionHandler:^(BOOL success) {
                    if (success) {
                        NSData *file = [NSData dataWithContentsOfURL:url];

                        NSString *docDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Import/iCloud"];
                        if (![[NSFileManager defaultManager] fileExistsAtPath:docDir])
                            [[NSFileManager defaultManager] createDirectoryAtPath:docDir withIntermediateDirectories:YES attributes:nil error:nil];

                        NSString *pdfFile = [docDir stringByAppendingPathComponent:filename];
                        if(![[NSFileManager defaultManager] fileExistsAtPath:pdfFile])
                            [[NSFileManager defaultManager] createFileAtPath:pdfFile contents:file attributes:nil];

                        NSLog(@"Successfully loaded data from cloud file name %@", filename);                        
                    }
                    else
                    {
                        NSLog(@"Failed to load data");
                    }
                }];

        }
    }
4

1 回答 1

1

看起来您在 iCloud 中有一个部分写入的文件(基于错误中的A Document Being Saved By XYZ),并且您的元数据查询已将其返回给您,因为它也与文件名匹配。几周前我遇到了类似的情况,并通过使用文件的确切路径来解决它,如下所示:

NSString *filepath = [containerURL.path stringByAppendingPathComponent:@"MyFileName"];

NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemPathKey, filepath];
[query setPredicate:pred];
于 2013-03-11T13:33:36.397 回答