0

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];
}
jrturton
  • 118,105
  • 32
  • 252
  • 268
chaithraVeeresh
  • 258
  • 3
  • 11
  • 1
    Hey could you add what you've tried already and maybe some code to this question? That would be helpful for people trying to answer your question. – Robert Karl May 07 '14 at 05:47
  • 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]; } but app crashng giving log asTerminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController: invalid scheme http. Only the file scheme is supported.' – chaithraVeeresh May 07 '14 at 05:55
  • Hey Jonathan just curious why you rolled back the edited version? It looked like a bit of an improvement. Also @chaithraVeeresh if you could add the code to the question that would be great. – Robert Karl May 07 '14 at 05:57
  • i added code what i've used in app . if i used that pdf file from bundlee its working fine – chaithraVeeresh May 07 '14 at 06:10

1 Answers1

2

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.

Diziet
  • 2,397
  • 1
  • 26
  • 34