I used iOS

Reposted from: http://blog.csdn.net/heroguo_jp/article/details/51134308



Preface 
There is a need in a recent project to use the content sharing function provided by the ios system on iOS devices to directly share physical content from third-party apps to our applications. The general principle is this, first registering for our iOS application can open document types (document type), and then in third-party applications, if they use the sharing function provided by iOS, then they will see our application , click to share. As for the specific ideas on the design and realization of requirements, I will explain them in detail in the next blog. This article is to talk about a set of unified specifications provided by Apple for better type identification in iOS and OS X systems, which is the "Uniform Type Identifier (UTI)" mentioned in the title. I put It translates into "Uniform Type Identifier", which is referred to as "UTI" uniformly below.

There are very few official tutorials 
on the use of UTI on the Internet, so I just refer to the explanations provided by Apple’s official documents. This blog should be my understanding of the official documents! ! For the part that I think is very important, I will post the original text of the official document, so that everyone can learn and understand, and will not be misled by my crooked words. At the same time, I also recommend you to search for some documents from the developer center. Here are a few recommendations : 
1.Cocoa Core Competencies – Uniform Type Identifier‍This 
document provides a view to explain what UTI is, how it works and who uses it. It is a very good novice guide. 
2. Introduction and use of Uniform Type 
Identifier‍This  document describes the basic concepts and attributes of UTI in detail, as well as their usage methods. The content is very rich. The main reference of this article is this
3. System-Declared Uniform Type Identifiers‍ 
This document provides a list of UTIs defined in the OS X system, and we can view the definition and meaning of each officially provided UTI. 
4. UTType Reference‍This 
document provides functions and methods for direct manipulation of UTI strings 
5. Add a custom document type and a new UTI for iOS applications step by step‍As the 
name suggests, this document explains how to import in iOS applications New UTI and adding custom document types.

Why there is UTI 
Why there is UTI, for example, it is like English is spoken as the official language of all countries in the world today. Why such a metaphor, because the Chinese speak Chinese as their mother tongue, and the French speak French as their mother tongue, but if a Chinese arrives in France and does not understand French, and the French people he meets do not understand Chinese, then how do they communicate? This is English comes in handy. Relatively speaking, the Apple operating system is equivalent to the whole world, and different programs or services are equivalent to various countries. Two different programs want to communicate with each other, such as sending files to each other, but one uses file extensions and the other uses MIME. Type, the data types of the two are different, cannot be parsed, and they do not know each other, so how to communicate? In such a situation, UTI will come into play, and it will act as the role of English in the real world.

The UTI concept 
Uniform type identifiers (UTIs) provides a unified way to identify data in the entire system, such as documents (documents), pasteboard data (clipboard data) and bundles (packages). As for the definition of UTI, the official document says, "A uniform type identifier is a string that uniquely identifies a class of entities considered to have a 'type'.". It roughly means that a uniform type identifier is a string that uniquely identifies an entity with a "type" attribute. Moreover, for this "type", the official document also gives us an example explanation. For a file or byte stream, "type" refers to the data type; for packages and bundles, "type" refers to What matters is their internal directory hierarchy.


In most cases, a UTI provides a unique identifier that all programs and services in the system can recognize and rely on. This may be too abstract. Let’s use the example given in the official document, such as a JPEG  Types of picture files, in different environments, can have the following different identification methods: 
1.'JPEG', OSType representation, 
2.'.jpg', file extension 
3.jpeg', file extension 
4. 'image/jpeg', a type in MIME (Multipurpose Internet Mail Extensions) 
and UTI is identified by the string 'public.jpeg', which completely replaces these inconsistent tags, and this string is like any other Old tags are fully compatible, and they can be converted to each other. Since UTI can identify any type of entity, they are more flexible than the old tags; using UTI we can represent the following entities: 
Pasteboard data 
Folders (directories) 
Bundles 
Frameworks 
Streaming data 
Aliases and symbolic links 
Usage 
1. Introduction 
Apple provides us with a set of common UTI strings in iOS and Mac applications, such as 'public.data', 'public.item', 'public.image', etc., which we can refer to them in the official documents meaning. In addition, we can also customize our own UTI string in the application, for example, we can define a UTI string that identifies a special document format called 'cc.icoc.shaobozheng', if other applications want to support For our documents in this format, they can use 'cc.icoc.shaobozheng' to identify our documents.

2. Character set 
See our example above, so let's talk about the character set used when defining UTI strings. Usually a UTI string is a Unicode string containing ASCII characters, and can also add Roman letters and Arabic numerals, such as (AZ), (az), (0-9) and dots (".") and hyphens ("-"). And any string containing illegal characters, such as underscore '_', cannot be used as UTI to identify content, and Apple will not give any error feedback.

3. Grammar 
Just like my example above, the definition of UTI is the same as when we fill in the organization when we develop iOS programs, and the anti-domain name rules are adopted. Such as the following types: 
com.apple.quicktime-movie 
com.mycompany.myapp.myspecialfiletype 
public.html 
com.apple.pict 
public.jpeg 
The domain names in UTI, such as 'com' and 'public', are only used to indicate the position of this UTI string in the domain name hierarchy, it does not affect any similar type of grouping. For example, the 'public' domain is what most applications use to identify standard types, and currently only Apple can create UTIs for the 'public' domain.

另外,我们可能会碰到的是一种‘dyn’域名,是动态域名,意思就是我们使用中,不会指定这种类型的UTI为某一个字符串,然后系统运行过程中,会自动识别帮我们处理。针对这种动态标识,我们是看不到的,但是我们可以通过UTI字符串的操作方式转换成我们的常用类型,比如OSType,MIME类型等。官方文档中,对动态标识有个比喻,“You can think of a dynamic identifier as a UTI-compatible wrapper around an otherwise unknown filename extension, MIME type, OSType, and so on”,大概意思就是我们可以把这种动态标识当做是针对普通类型进行了重写包装的,而且是兼容UTI的一种标识。

最后一种就是可以自定义的域名,代表性的就是‘com’域名,Apple也给我们提供了一些他们定义的’com’域名的UTI。

顺应性 
UTI相对于其他那些旧标签的一个关键优势就是在于,它可以在一个顺应结构中声明。而用我们面向对象的方式说,UTI就是可继承的,而且是多继承方式。先上图: 
Write picture description here

如上图所示,‘public.html’这个UTI就是继承于‘public.text’这个UTI,因为‘public.html’标识的是HTML文本格式,也属于是文本格式的一种,而文本、图片等等这些内容又都属于是数据的一种,所以他们继承于’public.data’这个UTI。

上面这个图示指的是UTI中的内容形式的继承结构,此外,原则上来说,指定UTI层次的时候,即可以指定它的功能结构,也可以指定它的物理结构,上图是就是一个内容结构图.物理结构指的就是这个UTI的物理实质,比如它标识一个目录,一个文件等,而功能结构指的就是这个UTI的用图,比如同样是文件,它标识的可以是图片、视频等等。 而一般指定UTI层次结构的规则是: 
1.一个UTI在物理层次上需要继承‘public.item’ 
2.一个UTI在功能层次需要继承非’public.item’之外的UTI。

然而,指定UTI的功能层次并不是强制的,但是这样做是考虑到可以更好地将UTI集成到系统一些特性中,就比如Spotlight应用,就可以把我们指定的功能性UTI和命名属性联系起来。下面是一个UTI功能顺应结构和物理顺应结构图: 
Write picture description here 
这个顺序性使得我们的UTI在决定类型上拥有更高的灵活性,不仅避免了大量的条件判断的使用,而且还可以关联你想不到的一些类型。

使用UTI 
应用场景 
在Mac OS中我们开发应用时我们可以经常使用到UTI,但是在开发iOS应用程序时,我们应用到UTI的场景不是很多,这也是现在网上教程偏少得原因。而在iOS开发中,一般我们使用UTI来标识剪贴板的类型。而在具体使用到Apple给我们提供的UTI字符串的时候,我们必须使用在UTCoreTypes.h文件中定义的常量来代替直接使用字符串。关于UIPasteboard的详细使用,大家可以去这篇博客中详细学习一下:精通UIPasteboard粘贴板。 
操作方法 
现在我们来看一下苹果提供的一些直接操作UTI的函数方法,简单列举几个。我们可以在MobileCoreServices这个framework中的UIType.h文件中找到,我们也可以仔细的看一下这个framework中的其他文件,都是对UTI的一些定义和生命。 
1.UITypeEqual 
判断俩个UTI是否完全一样,后者是一个动态标签说明是否是另外一个UTI标签说明的子集。

2.UITypeConformsTo 
判断俩个标签的顺应性,用面向对象的角度理解,就是判断是否是子类。

3.UTTypeCreatePreferredIdentifierForTag 
通过其他类型标识符,如MIME标识符,转换成UTI,当可以创建多个UTI字符串时,一般返回’public’域名的UTI。

4.UTTypeCreateAllIdentifiersForTag 
通过其他类型标识符,如MIME标识符,转换成UTI,当可以创建多个UTI字符串时,返回所有的UTIs,让你自己选

5.UTTypeCopyPreferredTagWithClass 
交换UTI字符串标识

自定义UTI 
用法 
苹果允许Mac开发者为他们的Mac App中独有的数据格式自定义新的UTI。它们一般被声明在下面几个文件中 
Info.plist 
Application bundles 
Spotlight Importer bundles 
Automator action bundles 
使用官方给我们的一个UTI声明的例子,Public.jpeg声明: 
UTExportedTypeDeclarations 


UTTypeIdentifier 
public.jpeg 
UTTypeReferenceURL 
http://www.w3.org/Graphics/JPEG/
UTTypeDescription 
JPEG image 
UTTypeIconFile 
public.jpeg.icns 
UTTypeConformsTo 

public.image 
public.data 

UTTypeTagSpecification 

com.apple.ostype 
JPEG 
public.filename-extension 

jpeg 
jpg 

public.mime-type 
image/jpeg 


一个UTI声明的属性列表: 
Value type 
Description

UTExportedTypeDeclarations 
array of dictionaries 
An array of exported UTI declarations (that is, identifiers owned by the bundle’s publisher). 
UTImportedTypeDeclarations 
array of dictionaries 
An array of imported UTI declarations (that is, identifiers owned by another company or organization). 
UTTypeIdentifier 
string 
The UTI for the declared type. This key is required for UTI declarations. 
UTTypeTagSpecification 
dictionary 
A dictionary defining one or more equivalent type identifiers. 
UTTypeConformsTo 
array of strings 
The UTIs to which this identifier conforms. 
UTTypeIconFile 
string 
The name of the bundle icon resource to associate with this UTI. 
UTTypeDescription 
string 
A user-visible description of this type. You can localize this string by including it in an InfoPlist.strings file. 
UTTypeReferenceURL 
string 
The URL of a reference document describing this type.

而且自定义的UTI必须指定UTExportedTypeDeclarations或者UTImportedTypeDeclarations,这样第三方App都可以使用,要么被导入到项目中的其他没有拥有这种数据格式的bundle中,这样都可以看得到。而官方文档中有一句话,“If both imported and exported declarations for a UTI exist, the exported declaration takes precedence over imported one”,我的理解是,被导出去得UTI声明所有人是定义UTI的发布者,而被导入的UTI声明所有人就是其他人,所以就是说如果对于一个UTI来说,俩种类型的声明都存在,则被导出的UTI声明优先使用。

Suggestions for custom UTI 
1. Your UTI string must be unique. The anti-domain naming method starting with 'com.' is a simple and effective way to ensure uniqueness. 
2. If your code depends on a third-party App, the UTI type may not be displayed in the system, you should declare it as an import type in the bundle 3. 
If your UTI type is a child of one or more existing UTI types class, you must add compliance to it so that it inherits from a parent class. It is best to inherit from the 'public' type.


Guess you like

Origin blog.csdn.net/dongtinghong/article/details/73075179