自定义UTI 注册你的APP所支持的文件类型

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

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

<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>文档类型名称
<string>pdf</string>
<key>LSHandlerRank</key> //是拥有此类型文档,还是仅用于打开
<string>Default</string>
</dict>
</array>

一般而言一个文档类型和一个文件类型对应,当然也可以是多个文件类型例如。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>LSItemContentTypes</key>
        <array>
            <string>com.sunsetlakesoftware.molecules.hr</string> 
            <string>org.gnu.gnu-zip-archive</string>
        </array>

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

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.plain-text</string>
            <string>public.text</string>
        </array>
        <key>UTTypeDescription</key>
        <string>Molecules Structure File</string>
        <key>UTTypeIdentifier</key>
        <string>com.sunsetlakesoftware.molecules.hr</string> // 自定义的type identifier
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key> //关键点
            <string>hr</string>
            <key>public.mime-type</key>  //关键点
            <string>chemical/x-pdb</string>
        </dict>
    </dict>
</array>

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

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

<dict>
   <key>CFBundleTypeName</key>
   <string>My File Format</string> //任意定义
   <key>CFBundleTypeIconFiles</key>  //icon图标资源,当用于显示此类文件的时候
       <array>
           <string>MySmallIcon.png</string>  //resource in bundle
           <string>MyLargeIcon.png</string>
       </array>
   <key>LSItemContentTypes</key>  //使用了UTI格式的,关联到文件格式
       <array>
           <string>com.example.myformat</string> //
       </array>
   <key>LSHandlerRank</key>
   <string>Owner</string> //非默认,而是owner
</dict>

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

这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值