Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files from UIDocumentPicker and wait until download has finished

I'm trying to download a file from the

-documentPicker:didPickDocumentAtURL:

method. I've tried to get the data of the file using

NSData *data = [NSData dataWithContentsOfURL:url];

but it didn't work how I expected, because the UIDocumentPicker fires the -documentPicker:didPickDocumentAtURL: -method before the file is downloaded.

How can I get the NSData from the file when it's downloaded?

Thanks in advance, Fabian.

like image 808
FTFT1234 Avatar asked Feb 23 '15 18:02

FTFT1234


1 Answers

The file actually is downloaded, but you need to use a file coordinator to read the file contents. Something like this should do:

- (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) {
        NSData *data = [NSData dataWithContentsOfURL:newURL];
        // Do something
    }];
    if (error) {
        // Do something else
    }
}
like image 101
spstanley Avatar answered Sep 20 '22 14:09

spstanley