如何在quicklook插件中获取网页?

时间:2013-07-15 15:59:13

标签: objective-c networking quicklook

我的插件必须发出谷歌请求,然后显示另一页的HTML内容 到目前为止,我只能显示HTML,即使QuickLook应该支持网络访问!

kng attempted to fetch pages(建立一个.webloc查看器),没有运气,然后在README中说道

  

Lion中的Quicklook现在完成了这个项目试图做的事情。

我有Mountain Lion并且已经应用​​了类似的模式,如下所示,仍然没有运气。网上似乎没有人试图从QuickLook插件访问URL;除了由Apple提供的未记录的WebLoc插件。此外,QuickLook编程指南 4岁

我尝试过:

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;
}

完整的代码&amp;环境can be found here

1 个答案:

答案 0 :(得分:0)

出于安全原因,不允许Quick Look插件执行任何网络请求。实际上,您的插件是沙盒的,只允许访问您尝试预览的文件(当然,除了插件所需的框架之外)。