iCloud: NSFileManager的startDownloadingUbiquitousItemAtURL的回调函数是什么?

19
我正在使用NSFileManagerstartDownloadingUbiquitousItemAtURL 方法将iCloud中的文件下载到本地(本地此时没有该文件的副本)。但是我找不到这个过程的回调函数。我需要回调函数来告诉我的应用程序所请求
2个回答

33

我相信这种方法是用于与基于苹果文档的文件协调器一起使用的。因此,您需要像这样使用文件协调器:

NSURL *itemURL = nil; // this is the URL you want to read from

__block NSData *data = nil;
NSError *error = nil;
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[coordinator coordinateReadingItemAtURL:itemURL options:0 error:&error byAccessor:^(NSURL *newURL) {
    data = [NSData dataWithContentsOfURL:newURL];
}];

这将是同步的,如果你想要异步执行某些操作,你可以使用块来实现:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // read info from the URL using the code above
    dispatch_async(dispatch_get_main_queue(), ^{
        // handle data read from the URL
    });
});

1
NSFileCoordinator中的方法在同步模式下工作得非常好。在项目的后期,我还需要异步版本-为此投了赞成票!谢谢。 - user523234
@SpaceDog,你说得一点没错。我一直在处理这些API,老实说,它们很糟糕。 - mattsven
5
苹果创建的API可以分为两类:1)那些很糟糕,非常混乱,模糊,不完整且文档很差;2)那些还过得去。没有一个API是设计得很好的。不要误解我,我喜欢苹果的产品,但是处理开发人员和API的苹果不是大家所知道的高标准苹果。恰恰相反。 - Duck
@SpaceDog 我现在正在打印这些评论,准备把它们挂在我的墙上。你完美地概括了我的想法。 - mattsven
1
我通常说苹果有两位CEO:蒂姆·库克负责我们所熟知的苹果,而撒旦则负责处理开发者相关事宜的苹果。 - Duck
需要注意的是,文件协调器将触发下载(因此不再需要调用 startDownloadingUbiquitousItemAtURL()),并在完成时调用该块,但遗憾的是没有进度回调。 - Nickkk

-1
要正确上传到iCloud,您还需要像这样的东西(其他任何东西都不起作用:|)
   NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

   NSString *FileNS = @"myfile.dat";
   NSURL *FileURL = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:FileNS ];


   BYTE *data = NULL;//Put your file data in here
   int FileSize = 0;//replace with your file size
   NSData *myData = [[NSData alloc] initWithBytes:data length:FileSize];
   [myData writeToFile:[FileURL path] atomically:YES];

用户要求下载,而不是上传。 - Duck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接