4

I try to show with QlPreviewController a pdf from internet url without download it. This is my code:

  NSURL* url2 = [NSURL URLWithString:@"http://wwww.myweb.com/files/terms_en.pdf"];

// Check if can be shown
if(![QLPreviewController canPreviewItem:url2]) {
    // cant show document
    NSLog(@"can't show document");
}else {
    NSLog(@"can show document");
   // Show the document in preview
  //  _previewItemURL = url;
    QLPreviewController* preview = [[QLPreviewController alloc] init];
    [preview setDataSource:url2];
}

But it didn't show anything. In addition I have a warning in the last sentence [preview setDataSource:url2] saying 'Sending 'NSURL *_strong' to parameter of incompatible type 'id

Anna
  • 189
  • 1
  • 8

2 Answers2

2

As per the documentation, QLPreviewController requires NSFileURL, i.e. local files. Download your web resource with other means (e.g. NSData dataWithContentsOfURL), write to disk, and then feed the local URL to it.

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67
-1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QLPreviewController *previewController = [[QLPreviewController alloc] init];
    [previewController setDataSource:self];
    [previewController setDelegate:self];
    previewController.currentPreviewItemIndex = indexPath.row;
    [self presentModalViewController:previewController animated:YES];
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return [self.arrayForPDFList count];
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    NSString *pathForPdf =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    pathForPdf = [pathForPdf stringByAppendingFormat:@"/%@.pdf",[self.arrayForPDFList objectAtIndex:index]];    
    return [NSURL URLWithString:pathForPdf];
}
Manu
  • 4,730
  • 2
  • 20
  • 45