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

iOS SDK:預覽和打開文檔

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

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í)行文件中為每個按鈕賦予一個動作,如下:

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

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

  3. }

選擇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é)議。

  1. #import <UIKit/UIKit.h>

  2. @interface MTViewController : UIViewController <UIDocumentInteractionControllerDelegate> 

  3. @end

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

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

#p#

看看previewDocument:方法的實現(xiàn),首先獲得文檔的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

如果現(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的方式顯示文檔。

  1. - (UIViewController *) documentInteractionControllerViewControllerForPreview:  

  2. (UIDocumentInteractionController *) controller { 

  3. 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,如下所示:

  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非常重要。原因很簡單,操作系統(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

 

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

2013-05-30 15:44:45

iOS開發(fā)iOS SDKUITextView

2018-02-06 22:11:41

微軟Office 功能

2011-08-02 11:07:42

iOS開發(fā) UIWebView

2013-05-17 10:54:37

iOS開發(fā)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

2009-07-07 08:51:00

微軟Windows 7新功能

2011-03-03 11:06:04

特性iOS 4.3

2009-09-01 13:13:28

C#打開Word文檔

2010-03-05 08:54:14

Windows 7試用版升級

2011-07-29 13:40:00

Xcode iOS 4.2 iPhone

2011-03-18 08:39:28

iOS 4.2 SDKiOS SDK

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技術棧公眾號

主站蜘蛛池模板: 精品国产欧美一区二区三区成人 | 中文字幕一区二区三区在线视频 | 欧产日产国产精品视频 | 精品国产精品国产偷麻豆 | 国产欧美一区二区在线观看 | 欧美一区二区三区国产精品 | 久久精品色欧美aⅴ一区二区 | 红桃视频一区二区三区免费 | 国产欧美一区二区三区另类精品 | 青青草网站在线观看 | 精品免费国产一区二区三区四区介绍 | 亚洲一区欧美 | 激情伊人网 | 亚洲国产成人精品女人久久久 | 欧美 日韩 国产 在线 | 中文字幕在线三区 | www.四虎.com | 久久久久国产一级毛片高清网站 | 国产成人精品亚洲日本在线观看 | 日本人做爰大片免费观看一老师 | 99精品视频一区二区三区 | 国产成人一区二区三区 | 欧美成人免费电影 | 在线婷婷| 日韩av成人 | 日韩视频在线免费观看 | 亚洲啊v | 国产精品久久久久久妇女6080 | 国产日韩一区二区三免费 | 91精品麻豆日日躁夜夜躁 | 亚洲一区二区三区高清 | 日韩国产中文字幕 | av无遮挡| 日韩精品一区二区三区中文字幕 | 欧美aaa一级片 | 一区二区三区日韩精品 | 91精品久久久久久久久久入口 | 精品永久 | 中文一区 | 亚洲精品第一国产综合野 | av免费网址 |