Reputation: 371
i want to download pdf file from webservice and open it with UIDocumentInteractionController.how can i do it? i use following code for download
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];
now what should i do to open it with UIDocumentInteractionController
Upvotes: 2
Views: 1344
Reputation: 49730
you can open PDF in UIDocumentInteractionController
using bellow code This is a example of displaying pdf from NSBundle
you can also displaying from Document directory like same as we display images. You need to full path of document and convert it into NSURL
using fileURLWithPath
:-
In yourViewController.h file:-
@interface yourViewController : UIViewController<UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *documentationInteractionController;
}
In **yourViewController.m file:-**
-(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];
}
Delegate of UIDocumentInteractionController
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
Demo link happy coding... :)
Upvotes: 3