如何将文件类型与 iPhone 应用程序相关联? [英] How do I associate file types with an iPhone application?

查看:40
本文介绍了如何将文件类型与 iPhone 应用程序相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于将您的 iPhone 应用程序与文件类型相关联的主题.

这个信息性问题中,我了解到应用可以与自定义 URL 协议相关联.

那是大约一年前,从那时起 Apple 推出了文档支持",它更进了一步,允许应用程序与文件类型相关联.文档中有很多讨论 关于如何设置您的应用以在遇到未知文件类型时启动其他合适的应用.这意味着关联对于任何应用程序都不是开箱即用的,就像 URL 协议注册所做的那样.

这让我想到了一个问题:Safari 或 Mail 等系统应用程序是否实现了这个系统来选择关联的应用程序,或者它们会像以前一样什么都不做?

解决方案

文件类型处理是 iPhone OS 3.2 的新功能,与现有的自定义 URL 方案不同.您可以注册您的应用程序来处理特定的文档类型,任何使用文档控制器的应用程序都可以将这些文档的处理交给您自己的应用程序.

例如,我的应用程序 Molecules(源代码可用)处理 .pdb 和.pdb.gz 文件类型,如果通过电子邮件或其他受支持的应用程序收到.

要注册支持,您需要在 Info.plist 中包含以下内容:

CFBundleDocumentTypes<阵列><字典><key>CFBundleTypeIconFiles</key><阵列><string>Document-molecules-320.png</string><string>Document-molecules-64.png</string></阵列><key>CFBundleTypeName</key><string>分子结构文件</string><key>CFBundleTypeRole</key><string>查看器</string><key>LSHandlerRank</key><string>所有者</string><key>LSItemContentTypes</key><阵列><string>com.sunsetlakesoftware.molecules.pdb</string><string>org.gnu.gnu-zip-archive</string></阵列></dict></阵列>

提供了两张图像,它们将用作邮件和其他能够显示文档的应用程序中受支持类型的图标.LSItemContentTypes 键可让您提供应用程序可以打开的统一类型标识符 (UTI) 数组.有关系统定义的 UTI 列表,请参阅 Apple 的 统一类型标识符参考.更多关于 UTI 的细节可以在 Apple 的 统一类型标识符概述.这些指南位于 Mac 开发人员中心,因为此功能已从 Mac 移植过来.

以上示例中使用的 UTI 之一是系统定义的,但另一个是特定于应用程序的 UTI.需要导出特定于应用程序的 UTI,以便系统上的其他应用程序可以知道它.为此,您需要向 Info.plist 添加一个部分,如下所示:

UTExportedTypeDeclarations<阵列><字典><key>UTTypeConformsTo</key><阵列><string>public.plain-text</string><string>public.text</string></阵列><key>UTTypeDescription</key><string>分子结构文件</string><key>UTTypeIdentifier</key><string>com.sunsetlakesoftware.molecules.pdb</string><key>UTTypeTagSpecification</key><字典><key>public.filename-extension</key><string>pdb</string><key>public.mime-type</key><string>化学/x-pdb</string></dict></dict></阵列>

此特定示例导出带有 .pdb 文件扩展名的 com.sunsetlakesoftware.molecules.pdb UTI,对应于 MIME 类型 chemistry/x-pdb.

有了这个,您的应用程序将能够处理附加到电子邮件或来自系统上其他应用程序的文档.在邮件"中,您可以点击并按住以显示可以打开特定附件的应用程序列表.

当附件打开时,您的应用程序将启动,您需要在 -application:didFinishLaunchingWithOptions: 应用程序委托方法中处理此文件.似乎以这种方式从 Mail 加载的文件被复制到您应用程序的 Documents 目录中,该目录位于与它们到达的邮箱对应的子目录下.您可以使用如下代码在应用程序委托方法中获取此文件的 URL:

NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];

请注意,这与我们用于处理自定义 URL 方案的方法相同.您可以使用如下代码将文件 URL 与其他 URL 分开:

if ([url isFileURL]){//处理传入的文件}别的{//处理自定义 URL 方案}

On the subject of associating your iPhone app with file types.

In this informative question I learned that apps could be associated with custom URL protocols.

That was almost one year ago and since then Apple introduced 'Document Support' which goes a step further and allows apps to associate with file types. There is a lot of talk in the documentation about how to set up your app to launch other appropriate apps when it encounters an unknown file type. This means the association doesn't work out of the box for any app, like the URL protocol registering did.

This leads me to the question: have system apps like Safari or Mail implemented this system for choosing associated applications, or will they do nothing, as before?

解决方案

File type handling is new with iPhone OS 3.2, and is different than the already-existing custom URL schemes. You can register your application to handle particular document types, and any application that uses a document controller can hand off processing of these documents to your own application.

For example, my application Molecules (for which the source code is available) handles the .pdb and .pdb.gz file types, if received via email or in another supported application.

To register support, you will need to have something like the following in your Info.plist:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array>
            <string>Document-molecules-320.png</string>
            <string>Document-molecules-64.png</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>Molecules Structure File</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.sunsetlakesoftware.molecules.pdb</string>
            <string>org.gnu.gnu-zip-archive</string>
        </array>
    </dict>
</array>

Two images are provided that will be used as icons for the supported types in Mail and other applications capable of showing documents. The LSItemContentTypes key lets you provide an array of Uniform Type Identifiers (UTIs) that your application can open. For a list of system-defined UTIs, see Apple's Uniform Type Identifiers Reference. Even more detail on UTIs can be found in Apple's Uniform Type Identifiers Overview. Those guides reside in the Mac developer center, because this capability has been ported across from the Mac.

One of the UTIs used in the above example was system-defined, but the other was an application-specific UTI. The application-specific UTI will need to be exported so that other applications on the system can be made aware of it. To do this, you would add a section to your Info.plist like the following:

<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>
        <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>

This particular example exports the com.sunsetlakesoftware.molecules.pdb UTI with the .pdb file extension, corresponding to the MIME type chemical/x-pdb.

With this in place, your application will be able to handle documents attached to emails or from other applications on the system. In Mail, you can tap-and-hold to bring up a list of applications that can open a particular attachment.

When the attachment is opened, your application will be started and you will need to handle the processing of this file in your -application:didFinishLaunchingWithOptions: application delegate method. It appears that files loaded in this manner from Mail are copied into your application's Documents directory under a subdirectory corresponding to what email box they arrived in. You can get the URL for this file within the application delegate method using code like the following:

NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];

Note that this is the same approach we used for handling custom URL schemes. You can separate the file URLs from others by using code like the following:

if ([url isFileURL])
{
    // Handle file being passed in
}
else
{
    // Handle custom URL scheme
}

这篇关于如何将文件类型与 iPhone 应用程序相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆