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

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

问题描述

关于将iPhone应用程式与档案类型相关联的主题。

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

信息性问题,我了解到,应用程序可以与自定义URL协议相关联。

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

这是差不多一年前,从那时起,苹果推出了文档支持,进一步,允许应用程序关联文件类型。有关的很多讨论文档,了解如何将应用设置为在遇到未知文件类型时启动其他适当的应用。这意味着关联不会开箱即用的任何应用程序,如URL协议注册。

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.

这导致我的问题:像Safari或Mail这样的系统应用程序实现这个系统来选择关联的应用程序,或者他们什么都不做?

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?

推荐答案

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

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.

例如,我的应用程序分子(其源代码可用)处理.pdb和.pdb.gz文件类型

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.

要注册支持,您需要在您的Info.plist中具有以下内容:

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>

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

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.

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

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>

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

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.

当附件打开时,您的应用程序将开始您将需要在 -application:didFinishLaunchingWithOptions:应用程序委托方法中处理此文件的处理。似乎以这种方式从Mail加载的文件被复制到您的应用程序的Documents目录下对应于他们到达什么电子邮件框的子目录。您可以在应用程序委托方法中使用以下代码获取此文件的URL:

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

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

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天全站免登陆