Download files from UIDocumentPicker and wait until download has finished
download, ios8, objective-c, uidocumentpickervc, xcode
Solution
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
}
}
Problem
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.