6

how to find the size of a document in iCloud before picking it and downloading to device using UIDocumentPickerViewController.I can download the document to the device and then converting to NSData,i can determine the file size,but can we avoid downloading the document itself and predetermine the file size before downloading .I could not find any method or property in uidocumentpickerviewcontroller.

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

 NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    NSError *error = nil;

    [coordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {

//assuming the file coordinator has downloaded the file here and provided the new url here
}

    }
sujith1406
  • 2,822
  • 6
  • 40
  • 60

1 Answers1

9

configure the file coordinator to just read the metadata only instead of downloading the entire file.From this url we get the filesize of a file in cloud using the method getPromisedItemResourceValue: forKey:NSURLFileSizeKey error:

Please post any answer that is more efficient than this way.This is the solution that worked for me

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
     NSError *error = nil;
    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];

     [coordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly error:&error byAccessor:^(NSURL *newURL) {

         NSError *err = nil;

         NSNumber * fileSize;
         if(![url getPromisedItemResourceValue:&fileSize forKey:NSURLFileSizeKey error:&err]) {
             NSLog(@"Failed error: %@", error);
             return ;
         } else {
          NSLog(@"fileSize %f",fileSize.doubleValue);
}
}
}
sujith1406
  • 2,822
  • 6
  • 40
  • 60
  • 1
    Do you have any idea how to do this for a folder. I'd like to get the list of all files in a folder, and their file sizes. But I don't want to download any of them. Is this possible? I have set document picker mode to `open`, and tried the approach you mentioned. Accessing the folder contents after calling start `startAccessingSecurityScopedResource` throws an error saying mentioned folder is not present in the location. – Suran Apr 14 '19 at 11:05