Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using built-in icons for mime type or UTI type in iOS

Problem:

I would like to be able to use the built-in iOS icons for standard mime types (or UTI types) in my listing of binary file content.

Background:

I have looked into using the new (since 3.2) document architecture, but using the UIDocumentInteractionController it seems that the assumption is that the actual binaries are already on the local device.

In my case I have a file listing from a remote server and know the mime type, name, title, etc for the remote file so I just want to show a file listing with icons (the actual binary is only loaded as needed).

The meta data I get from the server contains proper mime types for the binaries so in theory I just want to get the system icon based on the type.

Work around?

I have tried the following "hack" as a proof of concept and it seems to work but this doesn't seem like the best way to go...

//Need to initialize this way or the doc controller doesn't work
NSURL*fooUrl = [NSURL URLWithString:@"file://foot.dat"];
UIDocumentInteractionController* docController = [[UIDocumentInteractionController interactionControllerWithURL:fooUrl] retain];

UIImage* thumbnail = nil;
//Need to convert from mime type to a UTI to be able to get icons for the document
NSString *uti = [NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (CFStringRef)self.contentType, NULL)) autorelease];

//Tell the doc controller what UTI type we want
docController.UTI = uti;

//The doc controller now seems to have icon(s) for the type I ask for...
NSArray* icons = docController.icons;
if([icons count] > 0) {
    thumbnail = [icons objectAtIndex:0];
}
return thumbnail;
like image 331
John K Avatar asked May 03 '11 23:05

John K


2 Answers

You can create a UIDocumentInteractionController without needing to specify a URL. The header for the class says the icons are determined by name if set, URL otherwise.

UIDocumentInteractionController* docController = [[UIDocumentInteractionController alloc] init];
docController.name = @"foo.dat";
NSArray* icons = docController.icons;
// Do something with icons
...
[docController release];
like image 180
Ben Lings Avatar answered Oct 15 '22 10:10

Ben Lings


I tried Ben Lings's solution, but it didn't work on iOS6.1 in either the simulator or on my iPad3. You need to provide an NSURL to the UIDocumentInteractionController, but that URL doesn't need to exist. Its last path component just needs to have the extension that you want.

The following code worked for me

NSString *extension = @"pptx"; // or something else
NSString *dummyPath = [@"~/foo" stringByAppendingPathExtension:extension]; // doesn't exist
NSURL *URL = [NSURL fileURLWithPath:dummyPath];
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
NSArray *systemIconImages = documentInteractionController.icons;

return systemIconImages;
like image 24
Heath Borders Avatar answered Oct 15 '22 10:10

Heath Borders