概要点:
1. 你要注册(向ios/mac系统)申明app能够打开某种类型的文档,这样其他app才可能通过DIC(document interaction interface)把文件转给你app来打开
2. 注册就要在plist里声明: document types(我猜的),然后打开文本模式一看,果然对应了CFBundleDocumentTypes
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>文档类型名称
<string>pdf</string>
<key>LSHandlerRank</key> //是拥有此类型文档,还是仅用于打开
<string>Default</string>
</dict>
</array>
一般而言一个文档类型和一个文件类型对应,当然也可以是多个文件类型例如。doc/。docx是word文档在两个不同版本下的文件后缀。这样你可以把这两个文件类型组合在一个文档类型中
A uniform type identifier (UTI) is a string that identifies a class of entities with a type. UTIs are typically used to identify the format for files or in-memory data types and to identify the hierarchical layout of directories, volumes or packages
这里有个基本的对照表,文件后缀和UTI的串
https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
更多的对应可以参考:
Apple's Uniform Type Identifiers Overview
<key>LSItemContentTypes</key> <array> <string>com.sunsetlakesoftware.molecules.pdb</string> //app id+.pdb <string>org.gnu.gnu-zip-archive</string> </array>
这里我们使用了一个系统定义的。org。gnu。。。。另外一个是程序定义的UTI,程序定义的UTI需要导出,系统其他程序才能知道//
3. 需要为自定义的UtI在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> // 自定义的type identifier <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
UTI 是和.pdb后缀文件关联,且mime类型是
chemical/x-pdb
.
这样一来,在邮件程序中,tap且hold等待弹出候选程序列表,可以打开特定的文件。
当附件被打开,你的app就启动了。一般而言,至少要在application:didfinishelaunchingwithoptions中处理文件路径等,看起来就像是文件被copy到你app目录下,然后打开了:
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
再看另外一个例子,注释在其中
<dict> |
<key>CFBundleTypeName</key> |
<string>My File Format</string> //任意定义 |
<key>CFBundleTypeIconFiles</key> //icon图标资源,当用于显示此类文件的时候 |
<array> |
<string>MySmallIcon.png</string> //resource in bundle |
<string>MyLargeIcon.png</string> |
</array> |
<key>LSItemContentTypes</key> //使用了UTI格式的,关联到文件格式 |
<array> |
<string>com.example.myformat</string> // |
</array> |
<key>LSHandlerRank</key> |
<string>Owner</string> //非默认,而是owner |
</dict> |
也可以通过MIME来指定文件类型
CFBundleTypeMIMETypes |
"Document MIME types” |
|
Contains an array of strings. Each string contains the MIME type name you want to map to this document type. (In Mac OS X v10.4, this key is ignored if the |
更多信息:
https://developer.apple.com/library/ios/#documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-SW1
案例: 在iOS app中导入/导出文档
我们经常会用到的文件操作,又希望本地有文档保存,可以离线操作,其实ibook/iannotation之类多有类似的东西,就是本地library;有时候,还希望可以导出文件去另g外一个地方:
新建一个项目view based application:
打开nib文件,增加两控件:
检查绑定view和controller的准确性,选择file owner来查看:
我们接着拖两个文件到resource下面;pdf文件和jpg文件,在
OfflineReader-Info.plist 属性设置中, 设置 “Icon file” key 为 “icon.jpg”. 这样我们的程序有了图标,也有一个pdf作为本地resource目录下的文件
如何导出文档:
还是之前描述的,通过tap且hold可以激活其他app list,然后使用其他app来打开文件:以上是ios-iphone 邮件app的行为。如何在我们自己的app做到一样,可以把resource目录下的pdf传给其它app来打开呢?DIC是肯定要用到的:)
//Displays a menu for opening the document and anchors that menu to the specified view.
[documentController presentOpenInMenuFromRect:CGRectZero
inView:self.view
animated:YES];
在代码中指定了com。adobe。pdf,代表了pdf文档。其它常用的有public。html/public。jpeg。。。那么其他几个方法主要是为了满足uidci protocol的要求。你可以考虑在其中作点工作,例如删除本地文件/log等
- (void)viewDidLoad { [super viewDidLoad]; [self openDocumentIn]; }
程序加载的效果如上/如果选ibook
文档共享
在iOS中,两个方法,要么通过itunes要么通过程序间的交换。。。
我们先谈第一个方法,比较简单,通过plist修改下,声明可以通过itunes文档传输
<key>UIFileSharingEnabled</key>
<true/>
属性 alias是application support itunes files sharing=YES这样,CMD+R编译,重新在真机上运行,打开ituens: 点击APPs,首先看到Sync APP:这里列出了支持sync的app,往下拖动,是File sharing//
说明就是: 这里列出的app是支持设备和电脑之间通过itune文件传输的
注意:本例子所有测试在真机上进行,如果是模拟器,就没有ibook了。也没法测试和itunes链接的问题
当文件拖动到app后,会存储在default document目录下
Note the following:
#import "OfflineReaderAppDelegate.h" #import "OfflineReaderViewController.h" @implementation OfflineReaderAppDelegate @synthesize window; @synthesize viewController; -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { if (url != nil && [url isFileURL]) { [self.viewController handleDocumentOpenURL:url]; } return YES; }
Importing Self-Defined Documents
The previous section showed how to import well-known document types, such as PDF. What happens if you want to import your own self-defined document types? For example, you are writing a Sudoku game and wanted to implement your own file format for saving the state of a Sudoku game. In this case, your file might have the .sdk extension, which is only used by your application.
To ensure that your application can handle files with the .sdk extension, you will add the keys as shown in Figure 16 to the OfflineReader-Info.plist file.
Note that you added another key to the CFBundleDocumentTypes array. You set the LSItemContentTypes to a unique value, using the reverse domain name of your company and the type you are defining. Since this is a self-defined content type, you have to define it using the UTExportedTypeDeclarations key.
Note: For more information on UTI, refer to Apple’s documentations – “Introduction to Uniform Type Identifiers Overview”.
Press Command-R to test the application on a real device again. This time round, if your email contains a document of extension .sdk, you will see the icon of your application displayed next to the document name (see Figure 17). When you tap on the document name, you will see a list of options to open your documents with.