如何将文件类型与iPhone应用程序关联?

本文翻译自:How do I associate file types with an iPhone application?

On the subject of associating your iPhone app with file types. 关于将iPhone应用程序与文件类型相关联的主题。

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. 这意味着该关联对于任何应用程序都不是开箱即用的,就像URL协议注册一样。

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? 这就引出了一个问题:是否有像Safari或Mail这样的系统应用程序实现了该系统来选择关联的应用程序,还是像以前一样不执行任何操作?


#1楼

参考:https://stackoom.com/question/BdjT/如何将文件类型与iPhone应用程序关联


#2楼

BIG WARNING: Make ONE HUNDRED PERCENT sure that your extension is not already tied to some mime type. 大警告:确保百分之一百确保您的扩展名尚未绑定到某些mime类型。

We used the extension '.icz' for our custom files for, basically, ever, and Safari just never would let you open them saying "Safari cannot open this file." 我们的自定义文件使用扩展名“ .icz”,从根本上来说,Safari永远不会让您打开它们,说“ Safari无法打开此文件”。 no matter what we did or tried with the UT stuff above. 无论我们对上面的UT做了什么或尝试过什么。

Eventually I realized that there are some UT* C functions you can use to explore various things, and while .icz gives the right answer (our app): 最终,我意识到可以使用一些UT * C函数来探索各种事物,而.icz给出了正确的答案(我们的应用程序):

In app did load at top, just do this... 在应用程序中确实加载了顶部,只需执行此操作...

NSString * UTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, 
                                                                   (CFStringRef)@"icz", 
                                                                   NULL);
CFURLRef ur =UTTypeCopyDeclaringBundleURL(UTI);

and put break after that line and see what UTI and ur are -- in our case, it was our identifier as we wanted), and the bundle url (ur) was pointing to our app's folder. 然后在该行之后插入break,看看UTI和ur是什么-在我们的例子中,这是我们想要的标识符),并且捆绑包网址(ur)指向我们应用程序的文件夹。

But the MIME type that Dropbox gives us back for our link, which you can check by doing eg 但是Dropbox为我们提供链接的MIME类型,您可以通过执行以下操作进行检查

$ curl -D headers THEURLGOESHERE > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 27393  100 27393    0     0  24983      0  0:00:01  0:00:01 --:--:-- 28926
$ cat headers
HTTP/1.1 200 OK
accept-ranges: bytes
cache-control: max-age=0
content-disposition: attachment; filename="123.icz"
Content-Type: text/calendar
Date: Fri, 24 May 2013 17:41:28 GMT
etag: 872926d
pragma: public
Server: nginx
x-dropbox-request-id: 13bd327248d90fde
X-RequestId: bf9adc56934eff0bfb68a01d526eba1f
x-server-response-time: 379
Content-Length: 27393
Connection: keep-alive

The Content-Type is what we want. Content-Type是我们想要的。 Dropbox claims this is a text/calendar entry. Dropbox声称这是一个文本/日历条目。 Great. 大。 But in my case, I've ALREADY TRIED PUTTING text/calendar into my app's mime types, and it still doesn't work. 但就我而言,我已经尝试将文本/日历输入到应用程序的mime类型中,但仍然无法正常工作。 Instead, when I try to get the UTI and bundle url for the text/calendar mimetype, 相反,当我尝试获取文本/日历模仿类型的UTI和捆绑包网址时,

NSString * UTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
                                                                   (CFStringRef)@"text/calendar", 
                                                                   NULL);

CFURLRef ur =UTTypeCopyDeclaringBundleURL(UTI);

I see "com.apple.ical.ics" as the UTI and ".../MobileCoreTypes.bundle/" as the bundle URL. 我将“ com.apple.ical.ics”视为UTI,将“ ... / MobileCoreTypes.bundle /”视为捆绑包URL。 Not our app, but Apple. 不是我们的应用程序,而是苹果。 So I try putting com.apple.ical.ics into the LSItemContentTypes alongside my own, and into UTConformsTo in the export, but no go. 因此,我尝试将com.apple.ical.ics放到我自己的LSItemContentTypes中,并在导出时放到UTConformsTo中,但没有成功。

So basically, if Apple thinks they want to at some point handle some form of file type (that could be created 10 years after your app is live, mind you), you will have to change extension cause they'll simply not let you handle the file type. 因此,基本上,如果Apple认为他们希望在某种程度上处理某种形式的文件类型(可以在应用启用后10年创建,请注意),您将不得不更改扩展名,因为他们根本不会让您处理文件类型。


#3楼

File type handling is new with iPhone OS 3.2, and is different than the already-existing custom URL schemes. 文件类型处理是iPhone OS 3.2的新增功能,与现有的自定义URL方案不同。 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. 例如,如果通过电子邮件或在其他受支持的应用程序中接收到我的应用程序Molecules (提供源代码),则处理.pdb和.pdb.gz文件类型。

To register support, you will need to have something like the following in your Info.plist: 要注册支持,您需要在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. 提供了两个图像,它们将用作Mail和其他能够显示文档的应用程序中受支持类型的图标。 The LSItemContentTypes key lets you provide an array of Uniform Type Identifiers (UTIs) that your application can open. LSItemContentTypes密钥使您可以提供应用程序可以打开的统一类型标识符(UTI)数组。 For a list of system-defined UTIs, see Apple's Uniform Type Identifiers Reference . 有关系统定义的UTI的列表,请参阅Apple的《 统一类型标识符参考》 Even more detail on UTIs can be found in Apple's Uniform Type Identifiers Overview . 有关UTI的更多详细信息,请参阅Apple的“ 统一类型标识符概述” Those guides reside in the Mac developer center, because this capability has been ported across from the Mac. 这些指南位于Mac开发人员中心,因为此功能已从Mac移植过来。

One of the UTIs used in the above example was system-defined, but the other was an application-specific UTI. 上例中使用的UTI之一是系统定义的,而另一个是特定于应用程序的UTI。 The application-specific UTI will need to be exported so that other applications on the system can be made aware of it. 需要导出特定于应用程序的UTI,以便可以使系统上的其他应用程序知道它。 To do this, you would add a section to your Info.plist like the following: 为此,您可以像下面这样向您的Info.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>
        <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 . 此特定示例导出带有.pdb文件扩展名的com.sunsetlakesoftware.molecules.pdb UTI,对应于MIME类型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. 打开附件后,将启动您的应用程序,并且您需要在-application:didFinishLaunchingWithOptions:应用程序委托方法中处理此文件。 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: 看来,以这种方式从Mail加载的文件被复制到应用程序的Documents目录中,该目录位于它们所到达的电子邮件所对应的子目录下。您可以使用以下代码在应用程序委托方法中获取此文件的URL:

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

Note that this is the same approach we used for handling custom URL schemes. 请注意,这与我们用于处理自定义URL方案的方法相同。 You can separate the file URLs from others by using code like the following: 您可以使用以下代码将文件URL与其他URL分开:

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

#4楼

To deal with any type of files for my own APP, I use this configuration for CFBundleDocumentTypes: 为了为自己的APP处理任何类型的文件,我对CFBundleDocumentTypes使用以下配置:

    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>IPA</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.item</string>
                <string>public.content</string>
                <string>public.data</string>
                <string>public.database</string>
                <string>public.composite-content</string>
                <string>public.contact</string>
                <string>public.archive</string>
                <string>public.url-name</string>
                <string>public.text</string>
                <string>public.plain-text</string>
                <string>public.source-code</string>
                <string>public.executable</string>
                <string>public.script</string>
                <string>public.shell-script</string>
                <string>public.xml</string>
                <string>public.symlink</string>
                <string>org.gnu.gnu-zip-archve</string>
                <string>org.gnu.gnu-tar-archive</string>
                <string>public.image</string>
                <string>public.movie</string>
                <string>public.audiovisual-​content</string>
                <string>public.audio</string>
                <string>public.directory</string>
                <string>public.folder</string>
                <string>com.apple.bundle</string>
                <string>com.apple.package</string>
                <string>com.apple.plugin</string>
                <string>com.apple.application-​bundle</string>
                <string>com.pkware.zip-archive</string>
                <string>public.filename-extension</string>
                <string>public.mime-type</string>
                <string>com.apple.ostype</string>
                <string>com.apple.nspboard-typ</string>
                <string>com.adobe.pdf</string>
                <string>com.adobe.postscript</string>
                <string>com.adobe.encapsulated-​postscript</string>
                <string>com.adobe.photoshop-​image</string>
                <string>com.adobe.illustrator.ai-​image</string>
                <string>com.compuserve.gif</string>
                <string>com.microsoft.word.doc</string>
                <string>com.microsoft.excel.xls</string>
                <string>com.microsoft.powerpoint.​ppt</string>
                <string>com.microsoft.waveform-​audio</string>
                <string>com.microsoft.advanced-​systems-format</string>
                <string>com.microsoft.advanced-​stream-redirector</string>
                <string>com.microsoft.windows-​media-wmv</string>
                <string>com.microsoft.windows-​media-wmp</string>
                <string>com.microsoft.windows-​media-wma</string>
                <string>com.apple.keynote.key</string>
                <string>com.apple.keynote.kth</string>
                <string>com.truevision.tga-image</string>
            </array>
            <key>CFBundleTypeIconFiles</key>
            <array>
                <string>Icon-76@2x</string>
            </array>
        </dict>
    </array>

#5楼

In addition to Brad's excellent answer, I have found out that (on iOS 4.2.1 at least) when opening custom files from the Mail app, your app is not fired or notified if the attachment has been opened before. 除了Brad的出色回答外,我还发现(至少在iOS 4.2.1上)从Mail应用程序打开自定义文件时,如果以前已打开附件,则不会触发或通知您的应用程序。 The "open with…" popup appears, but just does nothing. 出现“打开方式为……”弹出窗口,但什么也不做。

This seems to be fixed by (re)moving the file from the Inbox directory. 这似乎是通过(从)“收件箱”目录中移动文件来解决的。 A safe approach seems to be to both (re)move the file as it is opened (in -(BOOL)application:openURL:sourceApplication:annotation: ) as well as going through the Documents/Inbox directory, removing all items, eg in applicationDidBecomeActive: . 一种安全的方法似乎是在打开文件时(在-(BOOL)application:openURL:sourceApplication:annotation: )(重新)移动文件,以及遍历Documents / Inbox目录,删除所有项目,例如applicationDidBecomeActive: That last catch-all may be needed to get the app in a clean state again, in case a previous import causes a crash or is interrupted. 万一先前的导入导致崩溃或中断,则可能需要使用最后一个汇总才能使应用再次处于干净状态。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值