4

In my app for iOS I store some file to the iCloud Drive folder for backup. Now I want to retrieve that file but i dont know which is the proper way for doing this. I mean if there is any specific method for iCloud Drive or i can just get it from the url.

I store the file to the iCLoud like this (https://stackoverflow.com/a/27358392/3065901):

- (void) storeToIcloud
{
   // Let's get the root directory for storing the file on iCloud Drive
   [self rootDirectoryForICloud:^(NSURL *ubiquityURL) {

        if (ubiquityURL) {

             // We also need the 'local' URL to the file we want to store

             NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
             NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
             NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myFile.xml"]; //Add the file name

             NSURL *localURL = [NSURL fileURLWithPath:filePath];

             // Now, append the local filename to the ubiquityURL
             ubiquityURL = [ubiquityURL URLByAppendingPathComponent:localURL.lastPathComponent];

             // And finish up the 'store' action
             NSError *error;
             if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:ubiquityURL error:&error]) {
                NSLog(@"Error occurred: %@", error);
             }
        }
        else {
            NSLog(@"Could not retrieve a ubiquityURL");
        }
   }];
}

- (void)rootDirectoryForICloud:(void (^)(NSURL *))completionHandler {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *rootDirectory = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"];

        if (rootDirectory) {
             if (![[NSFileManager defaultManager] fileExistsAtPath:rootDirectory.path isDirectory:nil]) {
                  NSLog(@"Create directory");
                  [[NSFileManager defaultManager] createDirectoryAtURL:rootDirectory withIntermediateDirectories:YES attributes:nil error:nil];
             }
        }

        dispatch_async(dispatch_get_main_queue(), ^{
             completionHandler(rootDirectory);
        });
    });
}

- (NSURL *)localPathForResource:(NSString *)resource ofType:(NSString *)type {
      NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
      NSString *resourcePath = [[documentsDirectory stringByAppendingPathComponent:resource] stringByAppendingPathExtension:type];
      return [NSURL fileURLWithPath:resourcePath];
}

How can I do for downloading the file from the iCloud drive?

Thanks in advance.

Community
  • 1
  • 1
user3065901
  • 4,678
  • 11
  • 30
  • 52

1 Answers1

2

You should use NSMetadataQuery to get the item from icloud, then when you have hooked a bookmark to the file use NSFileCoordination to read it, the same method can be used to read from the cloud or from your app sandbox... Everything is explained in detail here: Building Document Based Apps, and there's even some sample code you can follow. Hope this helps!

gbdavid
  • 1,639
  • 18
  • 40
  • Thanks for the reply. All that I see is written in swift. Do you know any documentation or tutorial in Objective C? – user3065901 Jun 29 '15 at 17:05
  • There's usually very little in apple's documentation (and elsewhere) about anything you want for ios and osx... My best guess will be to start from the NSMetadataQuery here (there's info for both swift and objective-c): https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMetadataQuery_Class/index.html and the File Metadata search Programming guide: https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/Introduction.html#//apple_ref/doc/uid/TP40001841 this last article is written only in Objective-C – gbdavid Jun 30 '15 at 07:27