預覽和打開PDF文檔

轉載:http://blog.csdn.net/kmyhy/article/details/9217745


http://mobile.tutsplus.com/tutorials/iphone/previewing-and-opening-documents-with-uidocumentinteractioncontroller/

iOS中的沙盒可以讓平臺更加的安全,這也是沙盒給用戶帶來的最主要好處。不過由於沙盒的嚴格限制,導致程序之間共享數據比較麻煩。一般在程序間共享文檔可以通過UIDocumentInteractionController(該類經常被開發者忽略)。本文中,我將介紹如何使用這個類在其它程序(已經安裝在設備中的程序)中預覽和打開文檔。
 
UIDocumentInteractionController
iOS 3.2中就已經存在了,使用起來非常靈活,功能也比較強大。它除了支持同設備上app之間的文檔分享外,還可以實現文檔的預覽、打印、發郵件以及複製。
 
UIDocumentInteractionController
的使用非常簡單。首先通過調用它唯一的類方法 interactionControllerWithURL:,並傳入一個URL(NSURL),來初始化一個實例對象。之後設置一下這個view controllerdelegate屬性,並實現一些恰當的delegate方法。
 
注意,UIDocumentInteractionController並不是UIViewController的子類,所以必須通知document interaction controller使用哪個view controller來預覽文檔。
 
Step 1: 設置項目
在同設備app之間打開文檔非常有用,一個常見的實例是在圖片編輯器中打開圖片,比如iPhoto
 
Xcode中創建一個新項目,選擇“Single View Application”模板。命名文檔,鍵入公司標識符,Device選擇iPhone,設備下邊選擇“Use Automatic Reference Counting”,其他兩項不選。然後點擊“Next”,保存項目,點擊“Creat”按鈕。


 
Step 2: 創建用戶界面
這個程序的用戶界面包括兩個按鈕,一個是用於在其他app中預覽PDF文檔,另一個是用戶在其他app中打開PDF文檔。創建用戶界面之前,在viewcontroller執行文件中爲每個按鈕賦予一個動作,如下:
1.- (IBAction)previewDocument:(id)sender {
2.}
 
1.- (IBAction)openDocument:(id)sender {
2.}

 
選擇MTViewController.xib,我們需要從右邊view controller視圖中拖拽兩個UIButton實例。選擇左邊的File’s Owner objectobject,打開Connections Inspector,把先前創建的動作和按鈕連接起來。
 

Step 3:預覽文檔
現在支持的是PDF文檔,你可以使用任何PDF文檔,但是在關於這個技巧的源文件中,我已經包含了一個PDF例子,就是蘋果的iOS編程指南,也可以在線獲得。把文檔拖至你的項目中,選中“ Copy items into destination group’s folder (ifneeded)”這個複選框,最後確保文件已經添加至下邊的“Documents target”中。

使用UIDocumentInteractionController類注意事項:1. 你需要保存着document interation controller的實例。2.需要實現UIDocumentInteractionControllerDelegate協議。
 
首先更新viewcontroller的頭文件(如下所示)來告訴compilerMTViewController類遵照UIDocumentInteractionControllerDelegate協議。
1.#import <UIKit/UIKit.h>
2.@interface MTViewController : UIViewController<UIDocumentInteractionControllerDelegate>
3.@end

 
view controller的實現文件中,添加一個私有屬性,類型爲UIDocumentInteractionController,並將名稱命名爲 documentInteractionController。這個屬性存儲着document interaction controller,之後會用着。
 
看看previewDocument:方法的實現,首先獲得文檔的URL (NSURL) ,由於文檔是app的一部分,因此通過NSBundle類獲得文檔的(NSURL)非常容易,如下:
 
1.- (IBAction)previewDocument:(id)sender {
2.    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample"withExtension:@"pdf"];
3.    if (URL) {
4.        // Initialize Document InteractionController
5.        self.documentInteractionController= [UIDocumentInteractionController 
interactionControllerWithURL:URL];
6.        // Configure Document InteractionController
7.        [self.documentInteractionControllersetDelegate:self];
8.        // Preview PDF
9.       [self.documentInteractionController presentPreviewAnimated:YES];
10.    }
11.}
 
如果返回了一個有效的URL,我們初始化一個UIDocumentInteractionController的實例,並且通過文檔的URL。在 documentInteractionController的屬性中我們存儲了一個document interaction controller的引用。view controller將會充當document interaction controllerdelegate
 
如果現在運行app,你會注意到點擊預覽按鈕時,什麼都沒有發生。因爲這裏我們首先要實現一個delegate method
 
前邊提到,UIDocumentInteractionController類並不是UIViewController的子類,而是繼承自 NSObject我們需要通知documentinteraction controller使用哪個view controller進行文檔預覽。
 
UIDocumentInteractionControllerDelegate
中有一個名documentInteractionControllerViewControllerForPreview:delegate方法,該方法請求獲得一個用於顯示(預覽)文檔的viewcontroller
 
我們希望在main viewcontroller中顯示預覽,所以可簡單的返回self,如下代碼所示。它的意思是document interfation controller將使用我們的view controller來預覽PDF文檔——modal view的方式顯示文檔。
 
1.- (UIViewController *)documentInteractionControllerViewControllerForPreview: 
(UIDocumentInteractionController *) controller {
2.    return self;
3.

 
當然你可以簡化documentInteractionControllerViewControllerForPreview:的實現以滿足你的需要。執行委託方法的同時,你可以首次運行app,試試看這個效果,你可以通過郵件分享這個文檔,可以打印或者複製。另外,也可以在支持該文檔類型的其app中打開文檔,在圖中點擊右邊的按鈕,看看我說的什麼意思。

Step 4: 打開文檔
爲了實現這一目的我們需要實現openDocument:方法。在openDocument:方法中,獲取到在程序bundle中一個PDF文件的url,用這個url初始化一個UIDocumentInteractionController

 

之後設置一下UIDocumentInteractionController delegate,在這個UIDocumentInteractionController中調用 presentOpenInMenuFromRect:inView:方法顯示一個菜單。傳入的第一個參數CGRectbuttonframe,如下所示:
1.- (IBAction)openDocument:(id)sender {
2.    UIButton *button = (UIButton *)sender;
3.    NSURL *URL = [[NSBundle mainBundle]URLForResource:@"sample" withExtension:@"pdf"];
4.    if (URL) {
5.        // Initialize Document InteractionController
6.        self.documentInteractionController= [UIDocumentInteractionController 
interactionControllerWithURL:URL];
7.        // Configure Document InteractionController
8.       [self.documentInteractionController setDelegate:self];
9.        // Present Open In Menu
10.        [self.documentInteractionControllerpresentOpenInMenuFromRect:[button frame] inView:self.view animated:YES];
11.    }
12.}

 
爲了測試openDocument:方法,在真機上運行實例app非常重要。原因很簡單,操作系統要檢查安裝的app是否支持我們要打開的文件類型(UTI)。如果沒有找到支持相應文件類型的app,那麼菜單中就不會有打開的提示,而這個不能通過iOS模擬器進行測試。
 
爲了測試這個功能,首先要在真機上安裝支持PDFapp,比如Dropbox或者AmazonKindle app

總結
使用UIDocumentInteractionController這個類可以簡單地實現app之間文檔的預覽和打開。建議你去看看這個類的參考文檔,特別是UIDocumentInteractionControllerDelegate協議——這裏面有許多delegate


方法,當遇到大文檔或者複雜的工作流時,這些方法都非常的方便。

源文件:

 

http://download.csdn.net/detail/ydj213/5405941


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章