Question

i want to display the pdf files as preview and display the PDF files in my app but pdfwhich i want to use are taking from the URL and display those pdf files in my document without using Web view but i want to display in UIDocumentation only. but app is crashing if i used from URL? how to solve this problem,?

NSURL *URL =  [NSURL URLWithString:@"http://golearning.blinkweb.com/uploads.00612104/00388749.pdf"];
if(URL)
{
    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
    self.documentInteractionController.delegate = self;
    [self.documentInteractionController presentPreviewAnimated:YES];
}
Was it helpful?

Solution

The answer here really lies in Chaithra's comment:

UIDocumentInteractionController: invalid scheme http. Only the file scheme is supported

To solve this problem you need to download the PDF file, obtain a local URL for it and can then pass it onto the UIDocumentInteractionController. Using AFNetworking as a baseline the following worked for me:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSProgress *progress = nil;
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  NSURL *documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
                                                                     inDomain:NSUserDomainMask
                                                            appropriateForURL:nil
                                                                       create:NO error:nil];
  return [documentsDirectory URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  [self displayDocument:filePath];
}];


[downloadTask resume];

My displayDocument: method looks like this:

- (void)displayDocument:(NSURL*)document {
  UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:document];
  documentInteractionController.delegate = self;
  [documentInteractionController presentPreviewAnimated:YES];
}

And the following delegate method:

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

Without using AFNetworking the code would be broadly similar as most of the classes used in the above sample are standard anyway. I happen to be using AFNetworking in this instance to simplify some of the setup and automatically using the categories it provides around UIProgress to update a progress bar for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top