关于将您的 iPhone 应用程序与文件类型相关联的主题。
在 this 信息性问题中,我了解到应用可以与自定义 URL 协议相关联。
那是差不多一年前的事了,从那时起,Apple 推出了“文档支持”,它更进了一步,允许应用程序与文件类型相关联。 documentation 中有很多关于如何设置您的应用程序以在遇到未知文件类型时启动其他适当应用程序的讨论。这意味着该关联不适用于任何应用程序,就像 URL 协议注册那样。
这让我想到了一个问题:Safari 或 Mail 等系统应用程序是否实现了这个系统来选择关联的应用程序,或者它们会像以前一样什么都不做?
文件类型处理是 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
}
除了 Brad 的出色回答外,我还发现(至少在 iOS 4.2.1 上)从 Mail 应用程序打开自定义文件时,如果之前打开过附件,您的应用程序不会被触发或通知。 “打开方式...”弹出窗口出现,但什么也不做。
这似乎可以通过(重新)从收件箱目录中移动文件来解决。一种安全的方法似乎是在打开文件时(在 -(BOOL)application:openURL:sourceApplication:annotation:
中)同时(重新)移动文件,以及通过 Documents/Inbox 目录删除所有项目,例如在 applicationDidBecomeActive:
中。如果之前的导入导致崩溃或中断,则可能需要最后一次全面了解应用程序以再次使应用程序处于干净状态。
-(BOOL)application:openURL:sourceApplication:annotation:
,即使对于已打开的附件也是如此。每次打开附件时,文件名后面都会加上一个数字,并递增为唯一的数字——test.text、test-1.txt、test-2.txt 等。
大警告:确保 100% 的扩展程序尚未绑定到某些 mime 类型。
我们为我们的自定义文件使用了扩展名“.icz”,基本上,Safari 永远不会让你打开它们说“Safari 无法打开这个文件”。无论我们对上面的 UT 内容做了什么或尝试了什么。
最终我意识到有一些 UT* C 函数可以用来探索各种事物,而 .icz 给出了正确的答案(我们的应用程序):
在应用程序中确实加载了顶部,只需执行此操作...
NSString * UTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(CFStringRef)@"icz",
NULL);
CFURLRef ur =UTTypeCopyDeclaringBundleURL(UTI);
并在该行之后放置 break 并查看 UTI 和 ur 是什么——在我们的例子中,它是我们想要的标识符),并且 bundle 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 和捆绑 URL 时,
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 年后创建),您将不得不更改扩展名,因为他们根本不会让您处理文件类型。
为了处理我自己的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>
-application:didFinishLaunchingWithOptions:
。- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url