IOS UTI Unified Type Identifier: Determine file type by suffix

Source: Internet
Author: User

Today in the learning document and data sharing, the first thing to do is to deal with the uniform type identifier UTI. See it for the first time, so write it down for it, first understand the concept of UTI and MIME

1. The same type identifier (Uniform type Identifier,uti) represents the central component of the iOS information share. It can be seen as the MIME type of the next generation. A UTI is a string that identifies resource types (images and text), and what types of information they develop will be used for common data objects, and they do not need to rely on old-fashioned indicators such as file extensions, MIME types, or file type metadata

, which shows part of Apple's basic compliance tree. Any item in a lower position on this tree must conform to all of its parent data properties. Declaring a parent UTI means supporting all of his sub-UTI. Therefore, applications that can open public.data must be able to open text, movies, image files, and so on. The name type of the UTI is public.data, etc.

2.MIME Understanding can go to Baidu Encyclopedia has the definition: http://baike.baidu.com/link?url=TQx8NxQPb8m5bsMWVR6p7NIFemdxyPh6RH_ Ug01ftknig7-iy4-tliuxvioxj-bavnowugjcixmeywo7vjrdpq

The MIME definition type is as follows: Text/xml is the suffix. The MIME type of the XML.

Common MIME Types (generic):

Hypertext Markup Language text. HTML text/html XML document. XML Text/xml 3. Mutual conversions between common file name extensionsThe first step is to add the Mobilecoreservices.framework framework, and add in the header file

#import <MobileCoreServices/MobileCoreServices.h>

The following are written in C language

(1) Suffix string converted to UTI string

-(NSString *) Preferredutiforextention: (NSString *) ext{    //Request the UTI via the file Extension    nsstring *theuti = (__bridge_transfer NSString *) Uttypecreatepreferredidentifierfortag ( Kuttagclassfilenameextension, (__bridge cfstringref) (EXT), NULL);     return Theuti;}

(2) using Kuitagclassmimetype as the first parameter, to Uitypecreatepreferredidentifierfortag (), is a MIME-type string converted to a UTI string

NSString *preferredutiformimetype (NSString *mime) {    //request the UTI via the file extention< /c3>    nsstring *theuti = (__bridge_transfer NSString *) Uttypecreatepreferredidentifierfortag ( Kuttagclassmimetype, (__bridge cfstringref) mime, NULL);     return Theuti;}

(3) using Uitypecopypreferredtagwithclass (), the UTI string is converted to a suffix extension

NSString *extensionforuti (NSString *auti) {    = (__bridge cfstringref) Auti;     = Uttypecopypreferredtagwithclass (Theuti, kuttagclassfilenameextension);     return (__bridge_transfer NSString *) results;}

(4) UTI string converted to MIME type

NSString *mimetypeforuti (NSString *auti) {    = (__bridge cfstringref) Auti;     = Uttypecopypreferredtagwithclass (Theuti, kuttagclassmimetype);     return (__bridge_transfer NSString *) results;}

(5) Testing compliance, using the Uitypeconformsto () function to test compliance. The function accepts two parameters: a source uti and a UTI to compare, and returns True if the first UTI complies with the second UTI. The equality test uses uitypeequal (), which shows an example of how compliance testing can determine if a file path might point to an image resource.

BOOL Pathpointstolikelyutimatch (NSString *path, Cfstringref Theuti) {NSString*extension =path.pathextension; NSString*preferreduti =preferredutiforextension (extension); return(Uttypeconformsto (__bridge cfstringref) Preferreduti, Theuti));} BOOL Pathpointstolikelyimage (NSString*path) {    returnPathpointstolikelyutimatch (Path, CFSTR ("Public.image"));} BOOL Pathpointstolikelyaudio (NSString*path) {    returnPathpointstolikelyutimatch (Path, CFSTR ("Public.audio"));}

(6) Get a list of compliance

Uttypecopydeclaration () is the most general (and most useful) function in all UTI functions in the iOS API, which returns a dictionary that contains the following keys.

"Kuttypeidentifierkey:uti name, he will be passed to the function (e.g.. Public.mpeg)

Kuttypeconformstokey: Any parent project with type compliance (e.g. PUBLIC.MPEG compliance Public.movie)

"Kuttypedescriptionkey: A realistic description of the type (if any) being considered (e.g." MPEG movie ")

"Kuttypetagspecificationkey: Equivalent ostype for a given UTI (such as MPG and MPEG), File extension (mpg, MPEG, MPE, M75, and M15) and MIME type (video/mpeg, video/mpg, video/ Dictionary of X-mpeg and video/x-mpg).

The following example mainly returns a dictionary up through the compliance tree to construct an array that represents all the items in a given UTI order. For example Public.mpeg type compliance Public.movie public.audiovisual-content Public.data Public.item and Public.content, the code is as follows:

Nsdictionary *utidictionary (NSString *Auti) {Nsdictionary*dictionary = (__bridge_transfer nsdictionary *) uttypecopydeclaration ((__bridge cfstringref) auti); returnDictionary;} Nsarray*uniquearray (Nsarray *Anarray) {Nsmutablearray*copiedarray =[Nsmutablearray Arraywitharray:anarray];  for(ID Object inchAnarray) {[Copiedarray Removeobjectidenticalto:Object]; [Copiedarray AddObject:Object]; }        returnCopiedarray;} Nsarray*conformancearray (NSString *Auti) {Nsmutablearray*results =[Nsmutablearray Arraywithobject:auti]; Nsdictionary*dictionary =utidictionary (Auti); IDconforms = dictionary[(__bridge NSString *) Kuttypeconformstokey]; //No Conformance    if(!conforms)returnresults; //Single Conformance    if([Conforms Iskindofclass:[nsstringclass] ]) {[Results Addobjectsfromarray:conformancearray (conforms)]; returnUniquearray (results); }        //iterate through multiple conformance    if([Conforms Iskindofclass:[nsarrayclass]])    {         for(NSString *eachutiinch(Nsarray *) conforms) [Results Addobjectsfromarray:conformancearray (Eachuti)]; returnUniquearray (results); }        //Just return the One-item array    returnresults;} Nsarray*allextensions (NSString *Auti) {Nsmutablearray*results =[Nsmutablearray array]; Nsarray*conformance =Conformancearray (Auti);  for(NSString *eachutiinchconformance) {Nsdictionary*dictionary =utidictionary (Eachuti); Nsdictionary*extensions = dictionary[(__bridge NSString *) Kuttypetagspecificationkey]; IDFileTypes = extensions[(__bridge NSString *) kuttagclassfilenameextension]; if([FileTypes Iskindofclass:[nsarrayclass]]) [Results addobjectsfromarray: (Nsarray*) FileTypes]; Else if([FileTypes iskindofclass:[nsstringclass]]) [Results addobject: (NSString*) FileTypes]; }        returnUniquearray (results);} Nsarray*allmimetypes (NSString *Auti) {Nsmutablearray*results =[Nsmutablearray array]; Nsarray*conformance =Conformancearray (Auti);  for(NSString *eachutiinchconformance) {Nsdictionary*dictionary =utidictionary (Eachuti); Nsdictionary*extensions = dictionary[(__bridge NSString *) Kuttypetagspecificationkey]; IDFileTypes = extensions[(__bridge NSString *) Kuttagclassmimetype]; if([FileTypes Iskindofclass:[nsarrayclass]]) [Results addobjectsfromarray: (Nsarray*) FileTypes]; Else if([FileTypes iskindofclass:[nsstringclass]]) [Results addobject: (NSString*) FileTypes]; }        returnUniquearray (results);}

IOS UTI Unified Type Identifier: Determine file type by suffix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.