来自网络节目的Pdf与QLPreviewController

时间:2013-02-13 09:39:45

标签: ios pdf qlpreviewcontroller

我尝试用QlPreviewController显示来自互联网网址的pdf而不下载它。 这是我的代码:

  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];
}

但它没有显示任何内容。另外我在最后一句[预览setDataSource:url2]中有一个警告说'发送'NSURL * _strong'到不兼容类型'id的参数

2 个答案:

答案 0 :(得分:2)

根据文档,QLPreviewController需要NSFileURL,即本地文件。使用其他方式(例如NSData dataWithContentsOfURL)下载您的Web资源,写入磁盘,然后将本地URL提供给它。

答案 1 :(得分:-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];
}