Question

I have a CSV file that I'm downloading from an S3 account and I would like to show it in my ios app by using the Quicklook framework.

The error I'm getting is in my console. It says

QLPreviewController's datasource shouldn't be nil at this point.

This appears after this line of code runs // Set data source [previewer setDataSource:self];

here's all the code for downloading the file, saving it and then loading with quicklook

-(void)showDocument
{
    NSString *stringURL = @"http://jornada.s3.amazonaws.com/Dust.csv";
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];
    if ( urlData )
    {
        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"tempfile.csv"];

        //[urlData writeToFile:filePath atomically:YES];

        BOOL newFile = [[NSFileManager defaultManager] createFileAtPath:filePath contents:urlData attributes:nil];

        arrayOfDocuments = [[NSArray alloc] initWithObjects:
                            filePath, nil];


        QLPreviewController *previewer = [[QLPreviewController alloc] init];

        [self addSubview:previewer.view];

        // Set data source
        [previewer setDataSource:self];

        // Which item to preview
        [previewer setCurrentPreviewItemIndex:0];
    }

}

/*---------------------------------------------------------------------------
 *
 *--------------------------------------------------------------------------*/
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
    return [arrayOfDocuments count];
}

/*---------------------------------------------------------------------------
 *
 *--------------------------------------------------------------------------*/
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{


    // Break the path into it's components (filename and extension)
    NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];

    NSArray *filePaths = [[fileComponents objectAtIndex:0] componentsSeparatedByString:@"/"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[((NSString *)[filePaths objectAtIndex:[filePaths count]-1]) stringByAppendingString:(NSString*)[fileComponents objectAtIndex:1]]];

    // Use the filename (index 0) and the extension (index 1) to get path

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];


    if (fileExists) {
        //
    }
    return [NSURL fileURLWithPath:filePath isDirectory:NO];
}
Was it helpful?

Solution

You don't add a preview controller's view to your view - you present the preview controller. And you should do that after setting the data source! So, in this order:

QLPreviewController* preview = [QLPreviewController new];
preview.dataSource = self;
[self presentViewController:preview animated:YES completion:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top