0

After my previous question, it seems that I gotta definitely deal with the fact that I have to use HTML in order to design interactive GUIs...but now the problem is another one: I know that for security reasons is not possible (unlike on Xcode 4.2 with OSX 10.6.8) to open/link anymore files from external directories using the <src> attribute, and I was wondering if there might be other ways to achieve that goal.

On this page ("Generating Enriched HTML" paragraph) is shown a portion of code that includes a CSS file as a MIME attachment: do you recon that it could be possible to obtain the same result with a different type of file (such a JS library or an image/video/audio)?

Here's some code from my project:

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
    @autoreleasepool {

        if (QLPreviewRequestIsCancelled(preview)) return noErr;

        NSMutableString *html=[[NSMutableString alloc] init];
        NSDictionary *props;

        props=@{
            (__bridge NSString *)kQLPreviewPropertyTextEncodingNameKey:@"UTF-8",
            (__bridge NSString *)kQLPreviewPropertyMIMETypeKey:@"text/html",
            };

        [html appendString:@"<html>"];
        [html appendString:@"<head>"];
        [html appendString:@"<script type=\"text/javascript\" src=\"JQuery.js\">"];
        [html appendString:@"</script>"];
        [html appendString:@"<script>"];
        //...
        [html appendString:@"</script>"];
        [html appendString:@"</head>"];
        [html appendString:@"<body>"];
        //...
        [html appendString:@"</body>"];
        [html appendString:@"</html>"];

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

    return noErr;
}

Thank you so much in advance!

Community
  • 1
  • 1

1 Answers1

2

Yes, all kinds of files (within the limits of how Quick Look sandboxes WebKit) can be attached using the cid: scheme. This is just a way to tell WebKit how to locate the data corresponding to a resource, so it can be used for all kinds of resources.

In your case, load JQuery.js in a NSData object, write src=\"cid:JQuery.js\" and add this to your props object for the kQLPreviewPropertyAttachmentsKey

@{
  @"JQuery.js" : @{ 
             (__bridge NSString *)kQLPreviewPropertyMIMETypeKey : @"text/javascript",
             (__bridge NSString *)kQLPreviewPropertyAttachmentDataKey: dataContainingJQuery
  },
},
Thomas Deniau
  • 2,488
  • 1
  • 15
  • 15
  • Thanks, Thomas for answering me...you've been very helpful! One last thing: if I don't want to look for the JS file inside a particular folder, what am I suppose to write into `dataWithContentsOfFile`? The tilde before the slash seems not working at all. – user3559460 May 30 '14 at 12:29
  • Store the JS file in your generator's bundle and use the same code as on Apple's sample : Use `+[NSBundle bundleForClass:]` to locate your bundle, then `-[bundle URLForResource:withExtension:]` to locate the JS and load it with `+[NSData dataWithContentsOfURL:]` – Thomas Deniau May 30 '14 at 12:41
  • I actually wrote this question because I knew that I could find a solution trying to replicate the Apple's sample code, but I kept having issues with its fake class `HTMLPreviewBuilder`: what am I supposed to substitute it with? – user3559460 May 31 '14 at 08:10
  • Any class that's part of the code that gets compiled in your bundle. It's just a way to locate the right bundle. – Thomas Deniau May 31 '14 at 13:17