iOS SDK:預(yù)覽和打開(kāi)文檔
iOS中的沙盒可以讓平臺(tái)更加的安全,這也是沙盒給用戶(hù)帶來(lái)的最主要好處。不過(guò)由于沙盒的 嚴(yán) 格限制,導(dǎo)致程序之間共享數(shù)據(jù)比較麻煩。一般在程序間共享文檔可以通過(guò)UIDocumentInteractionController(該類(lèi)經(jīng)常被開(kāi)發(fā)者忽略)。本文中,我將介紹如何使用這個(gè)類(lèi)在其它程序(已經(jīng)安裝在設(shè)備中的程序)中預(yù)覽和打開(kāi)文檔。
UIDocumentInteractionController在iOS 3.2中就已經(jīng)存在了,使用起來(lái)非常靈活,功能也比較強(qiáng)大。它除了支持同設(shè)備上app之間的文檔分享外,還可以實(shí)現(xiàn)文檔的預(yù)覽、打印、發(fā)郵件以及復(fù)制。
UIDocumentInteractionController 的使用非常簡(jiǎn)單。首先通過(guò)調(diào)用它唯一的類(lèi)方法interactionControllerWithURL:,并傳入一個(gè)URL(NSURL),來(lái)初始化一個(gè)實(shí)例對(duì)象。之后設(shè)置一下這個(gè)view controller的delegate屬性,并實(shí)現(xiàn)一些恰當(dāng)?shù)膁elegate方法。
注意,UIDocumentInteractionController并不是UIViewController的子類(lèi),所以必須通知document interaction controller使用哪個(gè)view controller來(lái)預(yù)覽文檔。
Step 1: 設(shè)置項(xiàng)目
在同設(shè)備app之間打開(kāi)文檔非常有用,一個(gè)常見(jiàn)的實(shí)例是在圖片編輯器中打開(kāi)圖片,比如iPhoto。
在Xcode中創(chuàng)建一個(gè)新項(xiàng)目,選擇“Single View Application”模板(圖1)。命名文檔,鍵入公司標(biāo)識(shí)符,Device選擇 iPhone,設(shè)備下邊選擇“Use Automatic Reference Counting”,其他兩項(xiàng)不選(圖2)。然后點(diǎn)擊“Next”,保存項(xiàng)目,點(diǎn)擊 “Creat”按鈕。
Step 2: 創(chuàng)建用戶(hù)界面
這個(gè)程序的用戶(hù)界面包括兩個(gè)按鈕,一個(gè)是用于在其他app中預(yù)覽PDF文檔,另一個(gè)是用戶(hù)在其他app中打開(kāi)PDF文檔。創(chuàng)建用戶(hù)界面之前,在view controller執(zhí)行文件中為每個(gè)按鈕賦予一個(gè)動(dòng)作,如下:
- (IBAction)previewDocument:(id)sender {
}
- (IBAction)openDocument:(id)sender {
}
選擇MTViewController.xib,我們需要從右邊view controller視圖中拖拽兩個(gè)UIButton實(shí)例(圖3)。選擇左邊的File’s Owner objectobject,打開(kāi)Connections Inspector,把先前創(chuàng)建的動(dòng)作和按鈕連接起來(lái)(圖4)。
Step 3:預(yù)覽文檔
現(xiàn)在支持的是PDF文檔,你可以使用任何PDF文檔,但是在關(guān)于這個(gè)技巧的源文件中,我已經(jīng)包含了一個(gè)PDF例子,就是蘋(píng)果的iOS編程指南,也可以在線(xiàn)獲得。把文檔拖至你的項(xiàng)目中,選中“ Copy items into destination group’s folder (if needed)” 這個(gè)復(fù)選框(圖5),最后確保文件已經(jīng)添加至下邊的“Documents target”中。
使用UIDocumentInteractionController類(lèi)注意事項(xiàng):
1. 你需要保存著document interation controller的實(shí)例。
2.需要實(shí)現(xiàn)UIDocumentInteractionControllerDelegate協(xié)議。
首先更新view controller的頭文件(如下所示)來(lái)告訴compiler,MTViewController類(lèi)遵照UIDocumentInteractionControllerDelegate協(xié)議。
#import <UIKit/UIKit.h>
@interface MTViewController : UIViewController <UIDocumentInteractionControllerDelegate>
@end
在view controller的實(shí)現(xiàn)文件中,添加一個(gè)私有屬性,類(lèi)型為UIDocumentInteractionController,并將名稱(chēng)命名為documentInteractionController。這個(gè)屬性存儲(chǔ)著document interaction controller, 之后會(huì)用著。
別走開(kāi)下頁(yè)為您帶來(lái)打開(kāi)文檔、總結(jié)、源文件下載
#p#
看看previewDocument:方法的實(shí)現(xiàn),首先獲得文檔的URL (NSURL) ,由于文檔是app的一部分,因此通過(guò)NSBundle類(lèi)獲得文檔的(NSURL)非常容易,如下:
- (IBAction)previewDocument:(id)sender {
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
if (URL) {
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentPreviewAnimated:YES];
}
}
如果返回了一個(gè)有效的URL,我們初始化一個(gè)UIDocumentInteractionController的實(shí)例,并且通過(guò)文檔的URL。 在 documentInteractionController的屬性中我們存儲(chǔ)了一個(gè) document interaction controller的 引用。view controller將會(huì)充當(dāng) document interaction controller的delegate
如果現(xiàn)在運(yùn)行app,你會(huì)注意到點(diǎn)擊預(yù)覽按鈕時(shí),什么都沒(méi)有發(fā)生。因?yàn)檫@里我們首先要實(shí)現(xiàn)一個(gè)delegate method。
前邊提到,UIDocumentInteractionController類(lèi)并不是UIViewController的子類(lèi),而是繼承自NSObject。我們需要通知document interaction controller使用哪個(gè)view controller進(jìn)行文檔預(yù)覽。
UIDocumentInteractionControllerDelegate 中有一個(gè)名documentInteractionControllerViewControllerForPreview:的delegate方法, 該方法請(qǐng)求獲得一個(gè)用于顯示(預(yù)覽)文檔的view controller。
我們希望在main view controller中顯示預(yù)覽,所以可簡(jiǎn)單的返回self,如下代碼所示。它的意思是 document interfation controller將使用我們的view controller來(lái)預(yù)覽PDF文檔—— 以modal view的方式顯示文檔。
- (UIViewController *) documentInteractionControllerViewControllerForPreview:
(UIDocumentInteractionController *) controller {
return self;
當(dāng)然你可以簡(jiǎn)化documentInteractionControllerViewControllerForPreview:的實(shí)現(xiàn)以滿(mǎn)足你的需要。執(zhí)行委托方法的同時(shí),你可以首次運(yùn)行app,試試看這個(gè)效果(圖6),你可以通過(guò)郵件分享這個(gè)文檔,可以打印或者復(fù)制。另外,也可以在支持該文檔類(lèi)型的其他app中打開(kāi)文檔,在圖7中點(diǎn)擊右邊的按鈕,看看我說(shuō)的什么意思。
Step 4: 打開(kāi)文檔
為了實(shí)現(xiàn)這一目的我們需要實(shí)現(xiàn)openDocument:方法。在previewDocument: 方法中,獲取到在程序bundle中一個(gè)PDF文件的url,用這個(gè)url初始化一個(gè)UIDocumentInteractionController。
之后設(shè)置一下UIDocumentInteractionController的delegate,在這個(gè) UIDocumentInteractionController中調(diào)用presentOpenInMenuFromRect:inView:方法顯示一個(gè)菜單。傳入的第一個(gè)參數(shù)CGRect是button的frame,如下所示:
- (IBAction)openDocument:(id)sender {
UIButton *button = (UIButton *)sender;
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
if (URL) {
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Present Open In Menu
[self.documentInteractionController presentOpenInMenuFromRect:[button frame] inView:self.view animated:YES];
}
}
為了測(cè)試openDocument:方法,在真機(jī)上運(yùn)行實(shí)例app非常重要。原因很簡(jiǎn)單,操作系統(tǒng)要檢查安裝的app是否支持我們要打開(kāi)的文件類(lèi)型(UTI)。如果沒(méi)有找到支持相應(yīng)文件類(lèi)型的app,那么菜單中就不會(huì)有打開(kāi)的提示,而這個(gè)不能通過(guò)iOS模擬器進(jìn)行測(cè)試。
為了測(cè)試這個(gè)功能,首先要在真機(jī)上安裝支持PDF的app,比如Dropbox或者Amazon的Kindle app。
總結(jié)
使用UIDocumentInteractionController這個(gè)類(lèi)可以簡(jiǎn)單地實(shí)現(xiàn)app之間文檔的預(yù)覽和打開(kāi)。建議你去看看這個(gè)類(lèi)的參考文檔,特別是UIDocumentInteractionControllerDelegate協(xié)議——這里面有許多delegate
方法,當(dāng)遇到大文檔或者復(fù)雜的工作流時(shí),這些方法都非常的方便。
源文件:
http://down.51cto.com/data/812631