今天在學習文件和資料共享中,首先講的處理統一型別識別符號UTI。第一次見,所以記下來以備之用,首先了解UTI和MIME的概念
1.同一型別識別符號(Uniform Type Identifier,UTI)代表IOS資訊共享的中心元件。可以把它看成下一代的MIME型別。UTI是標識資源型別(比如影象和文字)的字串,他們制定哪些型別的資訊將用於公共資料物件,他們不需要依賴於老式的指示符,比如副檔名,MIME型別,或者檔案型別的後設資料
如圖,顯示了Apple的基本順應樹的一部分。這個樹上位於較低位置的任何專案都必須順應其所有父資料屬性。宣告一個父UTI意味著支援他的所有子UTI。因此,可以開啟public.data的應用必須能開啟文字,電影,影象檔案等。其UTI的名稱型別就是public.data等
2.MIME的瞭解可以去百度百科上有定義:http://baike.baidu.com/link?url=TQx8NxQPb8m5bsMWVR6p7NIFemdxyPh6RH_uG01FTKNIg7-iy4-TLiUXVIOxj-BavNOWugJCixMEywo7vJrdPq
MIME的定義型別如下 如text/xml就是字尾.xml的MIME型別。
常見的MIME型別(通用型):
#import <MobileCoreServices/MobileCoreServices.h>
以下都用的是C語言編寫的
(1)字尾名字串轉化為UTI字串
-(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)使用kUITagClassMIMEType作為第一個引數,給UITypeCreatePreferredIdentifierForTag(),是MIME型別字串轉化為UTI字串
NSString *preferredUTIForMIMEType(NSString *mime) { //request the UTI via the file extention NSString *theUTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,(__bridge CFStringRef)mime, NULL); return theUTI; }
(3)使用UITypeCopyPreferredTagWithClass(),是UTI字串轉化為字尾副檔名
NSString *extensionForUTI(NSString *aUTI) { CFStringRef theUTI = (__bridge CFStringRef)aUTI; CFStringRef results = UTTypeCopyPreferredTagWithClass(theUTI, kUTTagClassFilenameExtension); return (__bridge_transfer NSString *)results; }
(4)UTI字串轉化為MIME型別
NSString *mimeTypeForUTI(NSString *aUTI) { CFStringRef theUTI = (__bridge CFStringRef) aUTI; CFStringRef results = UTTypeCopyPreferredTagWithClass(theUTI, kUTTagClassMIMEType); return (__bridge_transfer NSString *)results; }
(5)測試順應性,使用UITypeConformsTo()函式測試順應性。該函式接受兩個引數:一個源UTI和一個要比較的UTI,如果第一個UTI順應第二個UTI,就返回True。相等性測試則使用UITypeEqual(),下面顯示了一個示例,說明如何順應性測試,確定檔案路徑是否可能指向影象資源。
BOOL pathPointsToLikelyUTIMatch(NSString *path, CFStringRef theUTI) { NSString *extension = path.pathExtension; NSString *preferredUTI = preferredUTIForExtension(extension); return (UTTypeConformsTo((__bridge CFStringRef) preferredUTI, theUTI)); } BOOL pathPointsToLikelyImage(NSString *path) { return pathPointsToLikelyUTIMatch(path, CFSTR("public.image")); } BOOL pathPointsToLikelyAudio(NSString *path) { return pathPointsToLikelyUTIMatch(path, CFSTR("public.audio")); }
(6)獲取順應性列表
UTTypeCopyDeclaration()是IOS API中的所有UTI函式中最一般(並且最有用)的函式,它返回包含以下鍵的字典。
》kUTTypeIdentifierKey:UTI名稱,他將被傳遞給函式(例如.public.mpeg)
》kUTTypeConformsToKey:型別順應的任何父專案(例如 public.mpeg順應public.movie)
》kUTTypeDescriptionKey:正在考慮的型別(如果存在的話)的現實描述 (例如 “MPEG movie”)
》kUTTypeTagSpecificationKey:給定UTI的等價OSType(例如MPG和MPEG)、副檔名( mpg、mpeg、mpe、m75和m15)和MIME型別(視訊/mpeg、視訊/mpg、視訊/x-mpeg和視訊/x-mpg)的字典。
下面例子主要是返回字典向上通過順應性樹來構建一個陣列,表示給定UTI順序的所有專案.例如public.mpeg型別順應public.movie public.audiovisual-content public.data public.item 和public.content,程式碼如下:
NSDictionary *utiDictionary(NSString *aUTI) { NSDictionary *dictionary = (__bridge_transfer NSDictionary *)UTTypeCopyDeclaration((__bridge CFStringRef) aUTI); return dictionary; } NSArray *uniqueArray(NSArray *anArray) { NSMutableArray *copiedArray = [NSMutableArray arrayWithArray:anArray]; for (id object in anArray) { [copiedArray removeObjectIdenticalTo:object]; [copiedArray addObject:object]; } return copiedArray; } NSArray *conformanceArray(NSString *aUTI) { NSMutableArray *results = [NSMutableArray arrayWithObject:aUTI]; NSDictionary *dictionary = utiDictionary(aUTI); id conforms = dictionary[(__bridge NSString *)kUTTypeConformsToKey]; // No conformance if (!conforms) return results; // Single conformance if ([conforms isKindOfClass:[NSString class]]) { [results addObjectsFromArray:conformanceArray(conforms)]; return uniqueArray(results); } // Iterate through multiple conformance if ([conforms isKindOfClass:[NSArray class]]) { for (NSString *eachUTI in (NSArray *) conforms) [results addObjectsFromArray:conformanceArray(eachUTI)]; return uniqueArray(results); } // Just return the one-item array return results; } NSArray *allExtensions(NSString *aUTI) { NSMutableArray *results = [NSMutableArray array]; NSArray *conformance = conformanceArray(aUTI); for (NSString *eachUTI in conformance) { NSDictionary *dictionary = utiDictionary(eachUTI); NSDictionary *extensions = dictionary[(__bridge NSString *)kUTTypeTagSpecificationKey]; id fileTypes = extensions[(__bridge NSString *)kUTTagClassFilenameExtension]; if ([fileTypes isKindOfClass:[NSArray class]]) [results addObjectsFromArray:(NSArray *) fileTypes]; else if ([fileTypes isKindOfClass:[NSString class]]) [results addObject:(NSString *) fileTypes]; } return uniqueArray(results); } NSArray *allMIMETypes(NSString *aUTI) { NSMutableArray *results = [NSMutableArray array]; NSArray *conformance = conformanceArray(aUTI); for (NSString *eachUTI in conformance) { NSDictionary *dictionary = utiDictionary(eachUTI); NSDictionary *extensions = dictionary[(__bridge NSString *)kUTTypeTagSpecificationKey]; id fileTypes = extensions[(__bridge NSString *)kUTTagClassMIMEType]; if ([fileTypes isKindOfClass:[NSArray class]]) [results addObjectsFromArray:(NSArray *) fileTypes]; else if ([fileTypes isKindOfClass:[NSString class]]) [results addObject:(NSString *) fileTypes]; } return uniqueArray(results); }