0

I'm getting this behaviour using Xcode 8.0. The problem is, after downloading a file and storing it on documents directory (code provided below), QLPreviewController only displays document's name and size. The property currentPreviewItem returns the correct path document. What's even more strange, is that if I try to open that document from another controller in my app, it works fine. I've implemented both QLPreviewControllerDelegate and QLPreviewControllerDataSource.

Code for downloading and saving document:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSURL *url = [NSURL URLWithString:file[@"url"]];

    NSData *data = [NSData dataWithContentsOfURL:url];

    if (!data) {
        completion([NSError new]);
        return;
    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:AppName];
    path = [path stringByAppendingPathComponent:file[@"name"]];

    file[@"filePath"] = [NSURL fileURLWithPath:path];

    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
        completion(nil);
    });
});

Then, when user selects a document I use the content saved on @"filePath" to show QLPreviewController. I've tried pushing it and presenting it modally and in both cases it just displays a gray page with document's name and size.

worldofjr
  • 3,868
  • 8
  • 37
  • 49

2 Answers2

0

File wasn't showed because downloaded contents didn't contain file's extension. When I tried opening it in other controller, it worked because I temporary used another file name containing the correct extension. So I solved the problem adding the extension .pdf to downloaded files that didn't include it. This made QLViewController displaying correctly the file.

0

In my case, a file was created with the correct name, but the file contained an error message from the server, thus not representing the expected file structure.

Sjakelien
  • 2,255
  • 3
  • 25
  • 43