概要点:
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的串
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.pdb</string> // 自定义的type identifier <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> //关键点 <string>pdb</string> <key>public.mime-type</key> //关键点 <string>chemical/x-pdb</string> </dict> </dict></array>
com.sunsetlakesoftware.molecules.pdb
UTI 是和.pdb后缀文件关联,且mime类型是
chemical/x-pdb
.
这样一来,在邮件程序中,tap且hold等待弹出候选程序列表,可以打开特定的文件。
当附件被打开,你的app就启动了。一般而言,至少要在application:didfinishelaunchingwithoptions中处理文件路径等,看起来就像是文件被copy到你app目录下,然后打开了:
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
再看另外一个例子,注释在其中
<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>
|