2

I'm developing an app that provides the ability to store cloud documents. This app will have the option to import data from other apps using the new UIDocumentPickerViewController. Everything works fine and I'm able to show the picker view controller. This is the code that I'm using to import the file:

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {



if (controller.documentPickerMode == UIDocumentPickerModeImport) {

    dispatch_async(dispatch_get_main_queue(), ^{



        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

         NSString*  path=[NSString stringWithFormat:@"%@/MyHandyTap/%@/%@",documentsDirectory,self.cartella,[url lastPathComponent]];

        BOOL startAccessingWorked = [url startAccessingSecurityScopedResource];
        NSURL *ubiquityURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

        __block NSData *data;

        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;
        [fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {

            data = [NSData dataWithContentsOfURL:newURL];

            [data writeToFile:path atomically:YES];

        }];
        [url stopAccessingSecurityScopedResource];

    }
}

With this code I'm able to import a lot of different file formats (.pdf, .txt, .rtf, .doc etc) however if I try to import files .pages or .numbers files from iCloud the call to [NSData dataWithContentsOfURL:newURL] returns null.

Is it possibile to let users import these kinds of files?

I've red the documentation listed here https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/FileProvider.html#//apple_ref/doc/uid/TP40014214-CH18-SW2 and I've downloaded the example listed here : https://developer.apple.com/devcenter/download.action?path=/wwdc_2014/wwdc_2014_sample_code/newboxanintroductiontoiclouddocumentenhancementsinios8.0.zip however I'm not able to figure out how to solve this issue.

Thank you in advance for your help

Andrea

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
jiraya85
  • 428
  • 1
  • 5
  • 26

1 Answers1

3

Even I did not test it, but likely you get the problems, because documents of type .pages or .numbers are no single files, but bundles. So you cannot read them with -dataWithContentsOfURL:.

Did you check copying with -copyItemAtURL:toURL:error: (NSFileManager)?

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • Hi your answer pointed me to the right solution. I was trying to upload .pages and .numbers to a web server (in order to store them in a personal cloud). However, because those are not simple files but bundles I was having a lot of problems. Zipping them before the upload and unzipping them after the download solved my problems. – jiraya85 Mar 20 '15 at 14:46
  • Of course that is another way to handle the "bundle problem". – Amin Negm-Awad Mar 20 '15 at 19:14
  • Can you help with http://stackoverflow.com/questions/30613645/add-edit-in-exel-or-edit-photo-extension – Durgaprasad Jun 08 '15 at 11:06