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