自定义UTI注册自己的APP

之前有整理过关于《根据文件后缀打开APP》的文章 ,请先参考它,然后接下来学习,如何自定UTI。 
应用场景:APP 打开本公司自定义格式的文件,特殊的自定义后缀的文件。通过QQ 微信、邮箱等等接受到手机上的特殊文件,在打开时,主动调用自己的APP。

要点: 
1. 你要注册(向iOS/mac系统)申明app能够打开某种类型的文档,这样其他app才可能通过DIC(document interaction interface)把文件转给你app来打开 
2. 注册就要在plist里声明: document types(我猜的),然后打开文本模式一看,果然对应了CFBundleDocumentTypes

<key>CFBundleDocumentTypeskey>
<array>
<dict>
<key>CFBundleTypeNamekey>文档类型名称
<string>pdfstring>
<key>LSHandlerRankkey> //是拥有此类型文档,还是仅用于打开
<string>Defaultstring>
dict>
array>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

一般而言一个文档类型和一个文件类型对应,当然也可以是多个文件类型例如。doc/。docx是word文档在两个不同版本下的文件后缀。这样你可以把这两个文件类型组合在一个文档类型中 
A uniform type identifier (UTI) is a string that identifies a class of entities with a type. UTIs are typically used to identify the format for files or in-memory data types and to identify the hierarchical layout of directories, volumes or packages 
这里有个基本的对照表,文件后缀和UTI的串 
https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html 
更多的对应可以参考: 
Apple’s Uniform Type Identifiers Overview

<key>LSItemContentTypeskey>
        <array>
            <string>com.sunsetlakesoftware.molecules.hrstring> 
            <string>org.gnu.gnu-zip-archivestring>
        array>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

这里我们使用了一个系统定义的。org。gnu。。。。另外一个是程序定义的UTI,程序定义的UTI需要导出,系统其他程序才能知道 
3. 需要为自定义的UtI在plist中申明:

<key>UTExportedTypeDeclarationskey>
<array>
    <dict>
        <key>UTTypeConformsTokey>
        <array>
            <string>public.plain-textstring>
            <string>public.textstring>
        array>
        <key>UTTypeDescriptionkey>
        <string>Molecules Structure Filestring>
        <key>UTTypeIdentifierkey>
        <string>com.sunsetlakesoftware.molecules.hrstring> // 自定义的type identifier
        <key>UTTypeTagSpecificationkey>
        <dict>
            <key>public.filename-extensionkey> //关键点
            <string>hrstring>
            <key>public.mime-typekey>  //关键点
            <string>chemical/x-pdbstring>
        dict>
    dict>
array>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

关键是说明com.sunsetlakesoftware.molecules.hrUTI 是和.hr后缀文件关联,且mime类型是 chemical/x-pdb. 
这样一来,在邮件程序中,tap且hold等待弹出候选程序列表,可以打开特定的文件。 
当附件被打开,你的app就启动了。一般而言,至少要在application:didfinishelaunchingwithoptions中处理文件路径等,看起来就像是文件被copy到你app目录下,然后打开了:

再看另外一个例子,注释在其中

<dict>
   <key>CFBundleTypeNamekey>
   <string>My File Formatstring> //任意定义
   <key>CFBundleTypeIconFileskey>  //icon图标资源,当用于显示此类文件的时候
       <array>
           <string>MySmallIcon.pngstring>  //resource in bundle
           <string>MyLargeIcon.pngstring>
       array>
   <key>LSItemContentTypeskey>  //使用了UTI格式的,关联到文件格式
       <array>
           <string>com.example.myformatstring> //
       array>
   <key>LSHandlerRankkey>
   <string>Ownerstring> //非默认,而是owner
dict>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

也可以通过MIME来指定文件类型

你可能感兴趣的:(iOS)