iCloud: Callback for NSFileManager's startDownloadingUbiquitousItemAtURL?

北慕城南 提交于 2019-11-28 18:53:38

I believe that this method is intended to be used in conjunction with file coordinators based on Apple's documentation. So you would need to use a file coordinator like so:

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];
}];

This will be synchronous, however, so if you wanted to do something asynchronously, you could use blocks as so:

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
    });
});

To upload correctly to iCloud you're going to also need something like this (anything else didn't work :| )

   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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!