2013-07-27 112 views
1

我想從webservice下載pdf文件,並用打開它UIDocumentInteractionController .how我能做到嗎?我用下面的代碼下載UIDocumentInteractionController控制器問題

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"myurl/abc.pdf"]]; 
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"abcd.pdf"]; 
pdfData writeToFile:filePath atomically:YES]; 

現在我應該怎麼做才能與UIDocumentInteractionController打開

回答

3

您可以用波紋管代碼這是NSBundle你也可以顯示PDF的例子UIDocumentInteractionController打開PDF從Document目錄顯示,就像我們顯示圖像一樣。你需要的文件完整路徑和使用fileURLWithPath它轉換成NSURL: -

yourViewController.h文件: -

@interface yourViewController : UIViewController<UIDocumentInteractionControllerDelegate> 
{ 
    UIDocumentInteractionController *documentationInteractionController; 
} 

在** yourViewController.m文件: - **

-(void) viewDidAppear:(BOOL)animated { 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"abcd.pdf"]; 

      NSURL *pdfUrl = [NSURL fileURLWithPath:filePath]; 
      documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL:pdfUrl]; 
      documentationInteractionController.delegate = self; 
      [documentationInteractionController presentPreviewAnimated:YES]; 

     [super viewDidAppear:animated]; 
    } 

UIDocumentInteractionController

代表

Demo link 編碼快樂... :)

+0

Nitinbhai但我的應用程序是崩潰,因爲它會返回空路徑...其實我從web服務下載PDF文件,並存儲在文件的文件夾,並嘗試從主束抓取... – iKambad

+0

如果您將其存儲在文檔目錄文件夾中,並嘗試從NSbundle中捕捉那麼如何獲取路徑:) –

+0

是的,這就是問題所以我該怎麼辦?要麼將其存儲在主包中,要麼 – iKambad