Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the size of a document in iCloud before picking it and downloading to device using UIDocumentPickerViewController

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
}

    }
like image 312
sujith1406 Avatar asked Nov 29 '16 14:11

sujith1406


People also ask

How do I open a file picker in Swift?

Just create a UIDocumentPickerViewController with the appropriate types, implement the delegate, and you are done. From your project's capabilities, enable both the iCloud and the Key-Sharing . Click your button. The following menu will pop up ..


1 Answers

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);
}
}
}
like image 165
sujith1406 Avatar answered Nov 14 '22 21:11

sujith1406