2013-07-27 5 views
1

Я хочу загрузить pdf-файл из webservice и открыть его с помощью UIDocumentInteractionController. Как я могу это сделать? я использую следующий код для загрузкиПроблемы с контроллером 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

вы можете открыть PDF в UIDocumentInteractionController с помощью сильфона кода Ниже приведен пример отображения PDF из NSBundle вы также можете отображение из каталога Document аналогично отображению изображений. Вам нужно полный путь документа и преобразовать его в NSURL используя fileURLWithPath: -

В 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

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller { 
     return self; 
    } 

Demo link счастливым кодирования ... :)

+0

Nitinbhai но мое приложение аварии, потому что он будет возвращать нулевой путь ... На самом деле я могу скачать PDF из веб-сервиса и сохранить его в папке документа и попытаться извлечь из основного пучка ... – iKambad

+0

, если вы сохранили его в папке каталога документов и попытались перенести из NSbundle, то как получить путь :) –

+0

да, это проблема, и что мне делать? либо сохраните его на главном комплекте, либо – iKambad

Смежные вопросы