根据所选的tableview行在webview中打开pdf文件

Open pdf files in webview according to tableview row selected

我从 sqlite 数据库中检索所有数据。我可以让它们填充 table 视图。当我单击一行时,webview 打开但不显示 pdf file.The 视图只是纯白色背景。没有崩溃,没有错误,只是不显示任何内容。 这是我的代码。不确定我做错了什么。

#import "PdfTableVC.h"
#import "PdfVC.h"

@interface PdfTableVC ()

@end

@implementation PdfTableVC {
NSMutableArray *listOfPdf;
}

@synthesize pdfTable;

- (void)viewDidLoad {
[super viewDidLoad];
[self initDatabase];
[self getPoints];
}

#pragma mark - View lifecycle

-(void)initDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"my.db"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
    return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"my.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
{
    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}

-(void)getPoints{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"my.db"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{

    const char *sql = "SELECT * FROM file_geositi";

    sqlite3_stmt *searchStatement;


    if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK)
    {

        listOfPdf = [[NSMutableArray alloc] init];


        while (sqlite3_step(searchStatement) == SQLITE_ROW)
        {

            NSString *pdf = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement,0)];


            [listOfPdf addObject:pdf];


        }
    }
    sqlite3_finalize(searchStatement);
}
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listOfPdf count];
}

//Table View cell
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier =@"pdfCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil){
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [listOfPdf objectAtIndex:indexPath.row];

return cell;
}

//Detail View items
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showPDF"]) {
    NSIndexPath *indexPath = [self.pdfTable indexPathForSelectedRow];
     PdfVC *destViewController = (PdfVC *)segue.destinationViewController;
     destViewController.dataPdf= [listOfPdf objectAtIndex:indexPath.row];    
 }
}

PdfVC.h

#import <UIKit/UIKit.h>

@interface PdfVC : UIViewController

//UIWebView
@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property (nonatomic, strong) NSString *dataPdf;

@end

PdfVC.m

#import "PdfVC.h"

@interface PdfVC ()

@end

@implementation PdfVC
@synthesize webView;
@synthesize dataPdf;


- (void)viewDidLoad {
[super viewDidLoad];

//No sure about this and in fact makes the app crash   
NSString *pdfName = [NSString stringWithFormat:@"%@", [self.dataPdf description]];
NSString *path = [[NSBundle mainBundle] pathForResource:pdfName ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];


}


@end

有什么帮助吗?谢谢

好的,所以在查看了您的代码之后,我发现了以下内容:

  • 您正在将 PDF 名称存储在 SQLite 数据库中(我真的不知道 理解这样做的目的,但假设你需要) 名称 'pdf_1'、'pdf_2' 和 'pdf_3',但这些名称不匹配 在支持文件夹中使用 PDF 的实际文件名 这是'PDF 1'等
  • 其次,您只是将从数据库中提取的 PDF 的名称传递到 PdfVC,实际上什么都不做,您必须在此处将 PDF 实际加载到 webView,所以它实际上什么都不做。

解法:

  • 将您的 PDF 从 'PDF 1' 等重命名为 'pdf_1' 等,以便它们与您数据库中存储的实际名称匹配,或者重命名您数据库中的条目以匹配文件名PDF.
  • 其次,将以下代码放入 PdfVC 视图控制器的 viewDidLoad 方法中:

    NSString *path = [[NSBundle mainBundle] pathForResource:dataPdf ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webView loadRequest:request];
    

所有这些代码所做的就是它使用存储在 属性 dataPdf 中的名称(通过 segue 传递)从资源中获取正确的 PDF,然后将该 PDF 加载到webView.

我建议您在这里重新考虑实际数据库的用途,因为您所做的只是将 PDF 的名称存储到 table 中。它可能根本不需要,您可以在 PDFTableVC 视图控制器中维护一个 NSArray PDF 名称。

希望这能回答您的问题。