On Jun 12, 2010, at 6:18 AM, Ben wrote:

> Given an NSData object, with no context as to what it might be, is it 
> possible to find if it contains a document of recognised file type?

It is possible, but I suspect that you have to know what you are looking for. 
I.e., you must check the contents for various pattens that would identify the 
type, and you must have pre-knowledge of what those patterns are. There is no 
magic detection in the OS.

> I know this is possible for image files, by using the imageIO framework and 
> doing the following:
> 
> CGImageSourceRef imageSource = 
> CGImageSourceCreateWithData(myNSDataObjectGoesHere, NULL);
> NSString *uti = (NSString *)CGImageSourceGetType(imageSource);
> NSString *extension = [[NSWorkspace sharedWorkspace] 
> preferredFilenameExtensionForType:uti];
> CFRelease(imageSource);
> // Standard typed-in-mail-disclaimer here

Chances are, CGImageSourceGetType() is doing what I mention above, which is not 
too difficult since it only supports a fixed number of formats and clearly 
knows how to decode each one.

> I suppose I could take the long route of writing the data to disk, then using 
> the NSWorkspace method -typeOfFile:error:, but that seems a bit heavy-handed.

Since there is no magic detection, this will not work. There are various APIs 
that you can use to check for certain types, including images, PDFs, XML, 
Plists, an RTF. You are likely on your own when it comes to app-specific 
formats.

HTH,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to