成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iOS SDK:預覽和打開文檔

移動開發 iOS
UIDocumentInteractionController在iOS 3.2中就已經存在了,使用起來非常靈活,功能也比較強大。它除了支持同設備上app之間的文檔分享外,還可以實現文檔的預覽、打印、發郵件以及復制。

iOS中的沙盒可以讓平臺更加的安全,這也是沙盒給用戶帶來的最主要好處。不過由于沙盒的 嚴 格限制,導致程序之間共享數據比較麻煩。一般在程序間共享文檔可以通過UIDocumentInteractionController(該類經常被開發者忽略)。本文中,我將介紹如何使用這個類在其它程序(已經安裝在設備中的程序)中預覽和打開文檔。

UIDocumentInteractionController在iOS 3.2中就已經存在了,使用起來非常靈活,功能也比較強大。它除了支持同設備上app之間的文檔分享外,還可以實現文檔的預覽、打印、發郵件以及復制。

UIDocumentInteractionController 的使用非常簡單。首先通過調用它唯一的類方法interactionControllerWithURL:,并傳入一個URL(NSURL),來初始化一個實例對象。之后設置一下這個view controller的delegate屬性,并實現一些恰當的delegate方法。

注意,UIDocumentInteractionController并不是UIViewController的子類,所以必須通知document interaction controller使用哪個view controller來預覽文檔。

Step 1: 設置項目

在同設備app之間打開文檔非常有用,一個常見的實例是在圖片編輯器中打開圖片,比如iPhoto。

在Xcode中創建一個新項目,選擇“Single View Application”模板(圖1)。命名文檔,鍵入公司標識符,Device選擇 iPhone,設備下邊選擇“Use Automatic Reference Counting”,其他兩項不選(圖2)。然后點擊“Next”,保存項目,點擊 “Creat”按鈕。

Step 2: 創建用戶界面

這個程序的用戶界面包括兩個按鈕,一個是用于在其他app中預覽PDF文檔,另一個是用戶在其他app中打開PDF文檔。創建用戶界面之前,在view controller執行文件中為每個按鈕賦予一個動作,如下:

  1. - (IBAction)previewDocument:(id)sender { 

  2. - (IBAction)openDocument:(id)sender { 

  3. }

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

Step 3:預覽文檔

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

使用UIDocumentInteractionController類注意事項:

1. 你需要保存著document interation controller的實例。

2.需要實現UIDocumentInteractionControllerDelegate協議。

首先更新view controller的頭文件(如下所示)來告訴compiler,MTViewController類遵照UIDocumentInteractionControllerDelegate協議。

  1. #import <UIKit/UIKit.h>

  2. @interface MTViewController : UIViewController <UIDocumentInteractionControllerDelegate> 

  3. @end

在view controller的實現文件中,添加一個私有屬性,類型為UIDocumentInteractionController,并將名稱命名為documentInteractionController。這個屬性存儲著document interaction controller, 之后會用著。

別走開下頁為您帶來打開文檔、總結、源文件下載

#p#

看看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 Interaction Controller

  5.         self.documentInteractionController = [UIDocumentInteractionController  

  6. interactionControllerWithURL:URL]; 

  7. // Configure Document Interaction Controller

  8.         [self.documentInteractionController setDelegate:self]; 

  9. // Preview PDF

  10.         [self.documentInteractionController presentPreviewAnimated:YES]; 

  11.     } 

  12. }

如果返回了一個有效的URL,我們初始化一個UIDocumentInteractionController的實例,并且通過文檔的URL。 在 documentInteractionController的屬性中我們存儲了一個 document interaction controller的 引用。view controller將會充當 document interaction controller的delegate

如果現在運行app,你會注意到點擊預覽按鈕時,什么都沒有發生。因為這里我們首先要實現一個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的方式顯示文檔。

  1. - (UIViewController *) documentInteractionControllerViewControllerForPreview:  

  2. (UIDocumentInteractionController *) controller { 

  3. return self;

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

Step 4: 打開文檔

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

之后設置一下UIDocumentInteractionController的delegate,在這個 UIDocumentInteractionController中調用presentOpenInMenuFromRect:inView:方法顯示一個菜單。傳入的第一個參數CGRect是button的frame,如下所示:

  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 Interaction Controller

  6.         self.documentInteractionController = [UIDocumentInteractionController  

  7. interactionControllerWithURL:URL]; 

  8. // Configure Document Interaction Controller

  9.         [self.documentInteractionController setDelegate:self]; 

  10. // Present Open In Menu

  11.         [self.documentInteractionController presentOpenInMenuFromRect:[button frame] inView:self.view animated:YES]; 

  12.     } 

  13. }

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

為了測試這個功能,首先要在真機上安裝支持PDF的app,比如Dropbox或者Amazon的Kindle app。

總結

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

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

源文件:

http://down.51cto.com/data/812631

 

責任編輯:閆佳明 來源: cocoachina
相關推薦

2013-05-30 15:44:45

iOS開發iOS SDKUITextView

2018-02-06 22:11:41

微軟Office 功能

2011-08-02 11:07:42

iOS開發 UIWebView

2013-05-17 10:54:37

iOS開發iOS SDK調試技巧

2011-07-08 17:45:19

iPhone 文檔

2011-07-06 10:59:14

iOS 4 XCode iPhone

2013-04-27 10:07:51

飛利浦

2012-07-27 09:30:44

Windows Pho

2011-03-03 11:06:04

特性iOS 4.3

2009-07-07 08:51:00

微軟Windows 7新功能

2009-09-01 13:13:28

C#打開Word文檔

2011-07-29 13:40:00

Xcode iOS 4.2 iPhone

2011-03-18 08:39:28

iOS 4.2 SDKiOS SDK

2010-03-05 08:54:14

Windows 7試用版升級

2018-04-08 18:29:22

Windows 10組織工具時間軸

2011-01-27 10:11:42

Android 3.0

2011-07-26 17:39:53

Xcode iPhone 文檔

2011-07-22 10:01:58

IOS SDK Twitter

2011-08-16 15:17:44

IOS SDK

2012-01-01 22:07:28

jQMjQuery MobiHTHL5
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 97国产精品 | 国产精品69毛片高清亚洲 | 国产在线视频在线观看 | 国产第1页 | 久久久女女女女999久久 | 国产成人aⅴ | 极情综合网 | 亚洲精品国产成人 | 国产精品视频一区二区三区不卡 | 隔壁老王国产在线精品 | 成人精品福利 | 精品欧美一区二区中文字幕视频 | 99这里只有精品 | 国产精品视频一二三区 | 亚洲精品一区久久久久久 | 国产精品99久久久久久动医院 | 亚洲 中文 欧美 日韩 在线观看 | 羞羞在线视频 | 久久成人国产 | 国产女人叫床高潮大片免费 | 久久这里只有精品首页 | 欧洲成人免费视频 | 国产精品178页 | 91在线中文字幕 | 国产一区二区 | 欧美激情在线一区二区三区 | 精品久久久精品 | 成人免费视频观看视频 | 99九九久久 | 黄色a级一级片 | 久久精品日产第一区二区三区 | 国产亚洲欧美在线视频 | 国产精品永久久久久 | 伊人在线视频 | 国产成人精品一区二区三区在线 | 日韩免费网站 | 欧美激情在线精品一区二区三区 | 亚洲国产精品成人无久久精品 | 在线观看成年视频 | 亚洲一区二区在线播放 | 一级免费毛片 |