Question

I am using QLPreviewController to display files, many of which are actually plaintext, but QLPreviewController doesn't seem to know that. (Files with the .m extension are unable to be displayed, for example.)

Is there a way to tell QLPreviewController to treat a file as text? (I think it decides that it just can't view them at all.) I was looking into UTIs, but I'm not sure if that is the right direction. (I will know if the file is text, so I won't be guessing.)

Or do I have to save them with .txt extensions behind the scenes?

Was it helpful?

Solution

Well I don't see a way in the API to tell QLPreviewController which UTI is the right one for a file. But because QLPreviewController determines the UTI by looking at the file extension, you could copy the file, append ".txt" to it and then open this one instead.

In your QLPreviewControllerDatascource implementation:

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index; {
    NSString *file = @"source.m"
    NSString *tmp = [file stringByAppendingPathExtension:@".txt"]
    [[NSFileManager defaultManager]copyItemAtPath:file toPath:tmp error:NULL];
    return [NSURL fileURLWithPath:tmp];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top