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

时间:2010-05-05 14:59:23

标签: ios cocoa-touch

关于将iPhone应用与文件类型相关联的主题。

this提供信息的问题中,我了解到应用可能与自定义网址协议相关联。

那是差不多一年前的事情,从那以后,Apple推出了“文档支持”,它更进了一步,允许应用程序与文件类型相关联。 documentation中有很多关于如何设置应用以在遇到未知文件类型时启动其他相应应用的讨论。这意味着关联对于任何应用程序都不能开箱即用,就像URL协议注册那样。

这引出了一个问题:像Safari或Mail这样的系统应用程序是否实现了这个系统来选择相关的应用程序,还是像以前那样什么都不做?

4 个答案:

答案 0 :(得分:400)

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

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

要注册支持,您需要在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>

提供了两个图像,这些图像将用作Mail中支持的类型以及能够显示文档的其他应用程序的图标。 LSItemContentTypes键允许您提供应用程序可以打开的统一类型标识符(UTI)数组。有关系统定义的UTI的列表,请参阅Apple的Uniform Type Identifiers Reference。关于UTI的更多细节可以在Apple的Uniform Type Identifiers Overview中找到。这些指南位于Mac开发人员中心,因为此功能已从Mac端口移植。

上面示例中使用的一个UTI是系统定义的,但另一个是特定于应用程序的UTI。需要导出特定于应用程序的UTI,以便系统上的其他应用程序可以识别它。为此,您需要在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>

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

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

打开附件后,您的应用程序将启动,您需要在-application:didFinishLaunchingWithOptions:应用程序委托方法中处理此文件。看来,以这种方式从Mail加载的文件被复制到应用程序的Documents目录下与其到达的电子邮箱对应的子目录下。您可以使用以下代码在应用程序委托方法中获取此文件的URL:

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

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

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

答案 1 :(得分:24)

除了Brad的优秀答案之外,我还发现(至少在iOS 4.2.1上)从Mail应用程序打开自定义文件时,如果之前打开了附件,则不会触发或通知您的应用程序。出现“open with ...”弹出窗口,但什么也没做。

这似乎可以通过(重新)从Inbox目录中移动文件来解决。一种安全的方法似乎是(重新)移动文件(在-(BOOL)application:openURL:sourceApplication:annotation:中)以及通过Documents / Inbox目录,移除所有项目,例如在applicationDidBecomeActive:。如果之前的导入导致崩溃或被中断,则可能需要最后一次全部捕获以使应用程序再次处于干净状态。

答案 2 :(得分:17)

大警告:确保您的分机与某些哑剧类型无关,确保百分之百。

对于我们的自定义文件,我们使用了扩展名“.icz”,基本上,只是,Safari只是永远不会让你打开它们说“Safari无法打开这个文件”。无论我们用上面的UT做过什么或尝试过什么。

最终我意识到有一些UT * C函数可以用来探索各种各样的东西,而.icz给出正确的答案(我们的应用程序):

在应用程序确实加载到顶部,只需执行此操作...

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

并在该行之后放置并查看UTI和ur是什么 - 在我们的例子中,它是我们想要的标识符,并且捆绑url(ur)指向我们的应用程序的文件夹。

但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

Content-Type就是我们想要的。 Dropbox声称这是一个文本/日历条目。大。但在我的情况下,我已经将文本/日历转换成我的应用程序的mime类型,但它仍然无法正常工作。相反,当我尝试获取文本/日历mimetype的UTI和捆绑网址时,

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

CFURLRef ur =UTTypeCopyDeclaringBundleURL(UTI);

我将“com.apple.ical.ics”视为UTI,将“... / MobileCoreTypes.bundle /”视为捆绑URL。不是我们的应用程序,而是Apple。所以我尝试将com.apple.ical.ics放在我自己的LSItemContentTypes中,然后放到导出中的UTConformsTo中,但是没有去。

所以基本上,如果Apple认为他们想要在某种程度上处理某种形式的文件类型(可能是在你的应用程序上线10年后创建的,请注意),你将不得不改变扩展名,因为他们根本就不会让你处理文件类型。

答案 3 :(得分:0)

要为自己的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>