Objective
There is a requirement in the recent project to share entity content directly from third-party apps to our app using the content sharing features available on iOS devices from the iOS system. The general principle is this, first for our iOS app registration can open document types (file type), and then in third-party apps, if they use the sharing features provided by iOS, then you will see our app, click to share. and the specific ideas about the design and implementation of the requirements, I will explain in detail in the next blog post. This article is about Apple in iOS and OS X system in order to better type recognition, and provide a uniform set of specifications, that is referred to in the title "Uniform type Identifier (UTI)", I translated it into "uniform type identifier", the following unified abbreviation for " UTI. "
Official tutorials
There are few tutorials on how to use UTI online, so I'm just referring to the official Apple documentation, which is my understanding of the official documentation!! Since I think it is very important, I will post the original text of the official documents so that everyone can learn to understand, not to be misled by my crooked words, but also recommend that you search some documents from the developer Center, here are recommended several:
1.Cocoa Core competencies–uniform Type Identifier?
This document provides a view of what a UTI is, how it works and who is using it, and is a very good beginner's guide.
2.Uniform Type identifier introduction and use?
This document describes in detail the basic concepts and properties of UTI, and how they are used, the content is very rich, the main reference in this article is this
3.system-declared Uniform Type Identifiers?
This document provides a list of the UTI defined in the OS X system, and we can look at the definition and implications of each of the officially provided UTI.
4.UTType Reference?
This document provides a function method for direct manipulation of the UTI string
5. Step-by-Step Add custom document type and new UTI for iOS app?
As the name implies, this document explains how to import a new UTI in an iOS app and add a custom document type.
Why would there be a UTI?
Why there is a UTI, for example, it is like the world is now the official language of the English speaking. Why is this analogy, because the Chinese speak the Chinese language, the French speak French, but if a Chinese people to France, but do not know French, the French do not understand Chinese, then how they communicate, this is the English language. In contrast, the Apple operating system equivalent to the entire world, different programs or services equivalent to each country, two different programs want to communicate with each other, such as sending files to each other, but a use file extension, a MIME type, the two data types are different, can not parse, do not know each other, So how do you communicate? In this scenario, the UTI has a role to play, and it acts as a real-world English character.
UTI Concept
Uniform type Identifiers (UTIs) provides a unified way to identify data throughout the system, such as documents, Pasteboard data (clipboard), and bundles (packages). and specifically to the definition of UTI, the official document says, "a uniform type identifier is a string that uniquely identifies a class of entities considered to HAV e a ' type '. ". It probably means that a uniform type identifier is a string that uniquely identifies an entity that has a type attribute. Also, for this "type", the official documentation gives us an example of the type of data that "type" refers to for a file or a byte stream, whereas for packages and bundles, "type" refers to the hierarchy of directories within them.
UTI Uses
In most cases, a UTI provides a unique identifier that all programs and services in the system can identify and rely on, so it might be too abstract, so let's use an example from an official document, such as a JPEG-type picture file, in a different environment, There are several different ways to identify the following:
1. ' JPEG ', OSType said,
2. '. jpg ', file name extension
3.jpeg ', File extension
4. ' Image/jpeg ', a type in MIME (multi-purpose Internet Mail extension type)
The UTI is identified by the string ' Public.jpeg ', which completely replaces these inconsistent tags, which are fully compatible with any other old tag, and they can be converted to each other. Since UTI can identify any type of entity, they are more flexible relative to the old label, and with UTI we can represent the following entities:
Pasteboard data
Folders (Directories)
Bundles
Frameworks
Streaming data
Aliases and Symbolic Links
Usage
1. Introduction
Apple has provided us with a collection of UTI strings that are common in iOS and Mac applications, such as ' public.data ', ' public.item ', ' public.image ', and so on, which we can refer to in the official documentation. In addition, we can customize our own UTI strings in the application, such as we can define a UTI string that identifies a special document format called ' Cc.icoc.shaobozheng ', and if other applications want to support documents in this format, they can use ' CC. Icoc.shaobozheng ' to identify our documents.
2. Character Set
See our example above, so let's take a look at the character set used to define the UTI string. Usually a UTI string is a Unicode string that contains ASCII characters and can also include Roman and Arabic numerals, such as (A-Z), (A-Z), (0-9) a bit ("."). ) and the connector ("-"). Any string containing illegal characters, such as an underscore ' _ ', cannot be used as a UTI to identify content, and Apple will not have any error feedback.
3. Syntax
As I have shown above, the definition of UTI is the same as when we were developing iOS programs when we filled out organization, and we took the anti-domain rules. Here are some of the following:
Com.apple.quicktime-movie
Com.mycompany.myapp.myspecialfiletype
Public.html
Com.apple.pict
Public.jpeg
The domain names in the UTI, such as ' com ', ' Public ', are simply used to indicate the location of the UTI string in the domain hierarchy, and it does not affect any grouping of similar types. For example, the ' public ' domain name is what most applications use to identify standard types, and only the UTI that Apple can create a ' public ' domain name is currently available.
In addition, we may encounter is a ' dyn ' domain name, is a dynamic domain name, meaning we use, will not specify this type of UTI for a certain string, and then the system will automatically identify the process to help us handle. For this dynamic identity, we cannot see it, but we can convert the UTI string into our common type, such as the Ostype,mime type. In official documents, there is a metaphor for dynamic identity, "You can think of a dynamic identifier as a uti-compatible wrapper around an otherwise unknown filename ex Tension, MIME type, OSType, and so on ", probably means that we can think of this dynamic identity as a rewrite wrapper for a normal type and an identity that is compatible with UTI.
The last one is the domain name that can be customized, and the representative is the ' COM ' domain name, and Apple also gives us some of the UTI that they define as the ' COM ' domain name.
Compliance
One of the key advantages of UTI over other old tags is that it can be declared in a conforming structure. In our object-oriented approach, the UTI is inheritable and is a multi-inheritance approach. First:
As shown, ' public.html ' this uti is inherited from ' public.text ' this UTI, because ' public.html ' is the HTML text format, also belongs to the text format, and the text, pictures and so on are all part of the data, So they inherit the uti of ' public.data '.
The above illustration refers to the inheritance structure in the content form of the UTI, and, in principle, it is possible to specify its function structure when specifying the UTI level, or to specify its physical structure. is a content structure diagram. Physical structure refers to the physical nature of the UTI, such as it identifies a directory, a file, etc., and the functional structure refers to the UTI's diagram, such as the same file, which can be identified by images, videos and so on. The rules that generally specify the UTI hierarchy are:
1. A UTI needs to inherit ' Public.item ' at the physical level
2. A UTI needs to inherit from a UTI other than ' public.item ' at the functional level.
However, it is not mandatory to specify the functional hierarchy of UTI, but this is done by considering that the UTI can be better integrated into some of the features of the system, such as the spotlight application, to associate our designated functional UTI with named attributes. Below is a UTI functional compliance structure and physical compliance structure diagram:
This sequencing allows our UTI to have a higher level of flexibility in the decision type, not only to avoid the use of a large number of conditional judgments, but also to correlate some of the types you might not expect.
Using UTI
Application Scenarios
We can often use UTI when we develop our applications in Mac OS, but when we develop iOS applications, there are not many scenarios that we apply to UTI, which is why the online tutorials are so low. In iOS development, we typically use UTI to identify the type of clipboard. And when it comes to using the UTI strings that Apple gives us, we have to use constants defined in the UTCoreTypes.h file instead of using strings directly. For detailed use of Uipasteboard, you can go to this blog to learn more about: Proficient in uipasteboard paste board.
Operation method
Now let's take a look at some of the functions that Apple offers to directly manipulate the UTI, and simply list a few. We can find it in the UIType.h file in the framework of mobilecoreservices, and we can also take a closer look at the other files in this framework, all of which are definitions and lives of UTI.
1.UITypeEqual
Determine if the two UTI are exactly the same, the latter being a dynamic label stating whether it is a subset of another UTI label description.
2.UITypeConformsTo
Judging the compliance of the two tags, the object-oriented angle of understanding is to determine whether the sub-class.
3.UTTypeCreatePreferredIdentifierForTag
With other type identifiers, such as MIME identifiers, to a UTI, when multiple UTI strings can be created, typically return the ' public ' domain name of the UTI.
4.UTTypeCreateAllIdentifiersForTag
Convert to UTI by other type identifiers, such as MIME identifiers, and when multiple UTI strings can be created, return all UTIs and let yourself choose
5.UTTypeCopyPreferredTagWithClass
Interchange UTI String Identification
Custom UTI
Usage
Apple allows Mac developers to customize the new UTI for data formats unique to their Mac app. They are generally declared in the following files
Info.plist
Application Bundles
Spotlight Importer Bundles
Automator Action Bundles
Using an example of a UTI statement that is officially given to us, Public.jpeg declares:
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
A list of properties for a UTI declaration:
Value type
Description
Utexportedtypedeclarations
Array of dictionaries
An array of exported UTI declarations (so is, identifiers ow Ned by the bundle ' s publisher).
Utimportedtypedeclarations
Array of dictionaries
An array of imported UTI declarations (so is, identifiers O Wned by another company or organization).
Uttypeidentifier
String
The UTI for the declared type. This key was 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.
And the custom UTI must specify Utexportedtypedeclarations or Utimportedtypedeclarations so that third-party apps are available, It is either imported into the project or other bundles that do not have this data format, which can be seen. And in the official document there is a sentence, "If both imported and exported declarations for a UTI exist, the exported declaration takes precedence over IMP orted One ", my understanding is that being exported to UTI declares that everyone is the publisher of the definition of UTI, and that the imported UTI declares that everyone is someone else, so that if both types of declarations exist for a UTI, the exported UTI declaration takes precedence.
Recommendations for customizing UTI
1. Your UTI string must be unique. The name of the anti-domain name that begins with ' com. ' is a simple and effective way to ensure uniqueness.
2. If your code relies on a third-party App,uti type may not be present in the system, you should declare it as an import type in the bundle
3. If your UTI type is a subclass of one or more existing UTI types, you must add compliance to it to inherit from a parent class. It is best to inherit from the ' public ' type.
IOS UTI (uniform type Identifier) Open app based on file suffix