Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get QuickLook to show offline files?

Our iPad app can show Documents and save them offline when needed. I've got a QLPreviewController subclass named DocumentViewController (named DVC from now on) for showing them.

Workflow of the app: - The user clicks a name of a document and the DVC is pushed on to show the document. - The DVC downloads the file offline and shows it when done. (So the HTTP URL is downloaded, stored offline, and an offline URL is returned)

The weird thing is, is that only PDF files are working with the offline URL, and the rest crashes.. (it works with online links though)

I did some tests and when I put file:// before the offline link the app does not crash but the DVC is ging me some information about the file (like that it is a excel 97-2004 document).

So some info is transferred, but I can't figure out what the problem is.

Here are some screenshots and after that some code.

enter image description here

code: Note that Document is a model class with document properties like id, name, file type and url.

//DVC QLPreviewController dataSource method for returning url
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger)index
{
    [SaveHelper saveDocumentFileAndPropertyWithDocument:document];

    //[SaveHelper getDocumentFileWithDocument:document]; without file://
    //if I return document.documentUrl it is working with all files except iworks files)
    return [SaveHelper getDocumentFileAsPathWithDocument:document]; //with file://
}

//SaveHelper methods
+ (NSString *)documentFilePathWithDocument:(Document *)document
{
    return [[self documentFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.%@", DOCUMENT_FILE_PREFIX, document.documentId, document.documentType]];
}

+ (NSURL *)saveDocumentFileAndPropertyWithDocument:(Document *)document
{
    if([self saveDocumentPropertyWithDocument:document])
    {
        return [self saveDocumentFileWithDocument:document];
    }

    return nil;
}

+ (NSURL *)saveDocumentFileWithDocument:(Document *)document
{
    NSData *data = [NSData dataWithContentsOfURL:document.documentURL];

    NSString *fullPath = [self documentFilePathWithDocument:document];

   if([[NSKeyedArchiver archivedDataWithRootObject:data] writeToFile:fullPath atomically:YES])
   {
       return [NSURL fileURLWithPath:fullPath];
   }

    return nil;
}

+ (NSURL *)getDocumentFileWithDocument:(Document *)document
{
    return [NSURL fileURLWithPath:[self documentFilePathWithDocument:document]];
}

+ (NSURL *)getDocumentFileAsPathWithDocument:(Document *)document
{
    return [NSURL fileURLWithPath:[@"file://" stringByAppendingPathComponent:[[self getDocumentFileWithDocument:document] absoluteString]]];
}

If more code needed, just say.

EDIT:

When logging the URL passed trough the 'getDocumentFileAsPathWithDocument' method:

url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_20.pdf
url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_80.docx

Where the PDF file is working and the docx not

When I try to load an image(jpg) from local storage I get a black screen with this error message:

warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/System/Library/Frameworks/QuickLook.framework/DisplayBundles/Image.qldisplay/Image (file not found).
warning: No copy of Image.qldisplay/Image found locally, reading from memory on remote device.  This may slow down the debug session.

EDIT:

The webview does not work either with the local urls. PDF is fine but the office files gives an message "Unable to read Document, the file format is invalid". The iWorks documents give the same error as the quicklook. I think its somewhere at the save and load of the format, I savve them as a NSDATA but after that there is no hint for the iPad to see if it is for example a word document (only the extension).

like image 655
Justin Avatar asked Aug 12 '11 09:08

Justin


People also ask

How do I view a file in QuickLook?

Select a file you wish to view, such as a Microsoft Office document. The file should appear in a QuickLook window with all its content and formatting intact ( Figure B ). Try the same with other files, such as PDFs and graphics. Try an audio file to hear it and a video file to watch it ( Figure C ).

What is the QuickLook tool?

The free QuickLook tool can display a variety of file types without you having to open them or launch their applications. Viewing your various documents, photos, videos and other files in Windows typically requires opening them with their associated program. However, you may not have the right application needed to open the file.

How do I install the QuickLook plugin?

Next, open File Explorer. Select the first qlplugin file you wish to install and press the spacebar. At the QuickLook Plugin window, click the link for Click Here to Install this plugin. Do the same for any other plugin files that you downloaded ( Figure A ). Right-click on the QuickLook icon in the System Tray and select Quit.

What file types does QuickLook preview show the icon for?

On macOS 10.14.6 and MS Office 16.32 (latest version), quicklook previews only show the icon for .doc and .docx files.


2 Answers

You haven't posted your download code, but I believe that the problem is there. Files from Pages (.pages extension) aren't actual files, they are bundles, i.e. directories that contain files and show as a single item in Finder (look up a .pages file in Finder, right-click it and select 'Show contents'). Downloading a .pages file is actually like downloading a directory: it depends on the web server what kind of result you get but it's most likely an error page.

You could detect that it's a .pages file and try to download all of its contents manually, but you'd have to study the structure of the files to see if that's possible because it's unlikely that you can request the contents of the directory from a web server.

The results for the .ppt and .xls files look normal to me; I think it unlikely that the iPad can preview MS Office documents at all.

Edit: apologies, I just read that iOS can preview MS Office documents. Perhaps the documents get somehow corrupted during download? Have you tried to set your download location to the app's documents folder and enable iTunes file sharing? That way you can download some documents, pull them off your device and then try to open it on your PC to see if that works.

like image 53
Desmond Avatar answered Oct 13 '22 21:10

Desmond


We finally found the solution!

I was right that the problem was with saving the document.

I needed to change the save method in:

+ (NSURL *)saveDocumentFileWithDocument:(Document *)document
{
    NSData *data = [NSData dataWithContentsOfURL:document.documentURL options:NSDataReadingUncached error:nil];

    NSString *fullPath = [self documentFilePathWithDocument:document];

    if([[NSFileManager defaultManager] createFileAtPath:fullPath contents:data attributes:nil])
    {
        return [NSURL fileURLWithPath:fullPath];
    }
//OLD CODE
//   if([[NSKeyedArchiver archivedDataWithRootObject:data] writeToFile:fullPath atomically:YES])
//   {
//       return [NSURL fileURLWithPath:fullPath];
//   }

    return nil;
}

SO saving it with the filemanager and not with a keyedarchiver.

like image 30
Justin Avatar answered Oct 13 '22 21:10

Justin