Question

My plugin has to make a google request and then display another page's HTML content.
I have only been able to display HTML so far, even though QuickLook is supposed to support network access!

kng attempted to fetch pages (building a .webloc viewer), with no luck, then said in the README

Quicklook in Lion now does what this project attempted to do.

I have Mountain Lion and have applied similar patterns, as shown below, with still no luck. Nobody on the web seem to have ever tried to access an URL from a QuickLook plugin; besides the undocumented WebLoc plugin provided by Apple. Moreover, the QuickLook Programing Guide is 4 years old.

What I have tried:

OSStatus GeneratePreviewForURL(void *thisInterface,
                               QLPreviewRequestRef preview,
                               CFURLRef url,
                               CFStringRef contentTypeUTI,
                               CFDictionaryRef options)
{
    if (QLPreviewRequestIsCancelled(preview))
        return noErr;

    //// Fetch Web Page

    NSData *data;
    NSError *err;

    /// Attempt 0: static HTML -- ok
    //NSString *html = @"<html><body><h1>It works!</h1></body></html>";

    /// Attempt 1: dataWithContentsOfURL -- ko
    data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://google.com/"] options:NSDataReadingUncached error:&err];

    /// Attempt 2: NSURLConnection -- ko
    //NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]];
    //NSURLResponse *response;
    //data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&err];


    // Builds /Users/pete/out.txt on error
    NSString *html = [[NSString alloc] initWithData:(NSData *)data encoding:NSUTF8StringEncoding];
    [err.description writeToFile:@"/Users/pete/out.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

    //// Display HTML

    NSMutableDictionary *props;
    [props setObject:@"UTF-8" forKey:(NSString *)kQLPreviewPropertyTextEncodingNameKey];
    [props setObject:@"text/html" forKey:(NSString *)kQLPreviewPropertyMIMETypeKey];

    QLPreviewRequestSetDataRepresentation(preview,
                                          (__bridge CFDataRef)[html dataUsingEncoding:NSUTF8StringEncoding],
                                          kUTTypeHTML,
                                          (__bridge CFDictionaryRef)props);
    return noErr;
}

The complete code & environment can be found here

Was it helpful?

Solution

For security reasons, Quick Look plug-ins are not allowed to do any network requests. In fact, your plug-in is sandboxed and should only be allowed to access the file you are trying to preview (in addition to the frameworks your plug-in needs, of course).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top