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.
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
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With