在webview中显示PDF文件

时间:2013-03-08 07:10:20

标签: iphone

我正在做一个应用程序。我正在webview中显示pdf文件,如下所示

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:@"KeyToSelectedFile"]];
NSString *filename1=[fileName stringByAppendingPathComponent:s1];

NSLog(@"%@",filename1);
NSURL *url = [NSURL fileURLWithPath:filename1];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
web.delegate=self;
[web loadRequest:request];
 web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];

但是当我第一次点击打开它会崩溃应用程序,从下次开始它正确打开。并且第一次出现错误,如

dyld: fast lazy binding from unknown image

所以请告诉我如何在uiwebview中显示pdf而不会崩溃。

4 个答案:

答案 0 :(得分:1)

我查了一遍,工作正常,试试

UIWebView * pdfWebView = [[UIWebView alloc] initWithFrame:'your Frame'];
NSArray * Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * bundlePath = [[Paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",yourFileName]];

if([[NSFileManager defaultManager] fileExistsAtPath:bundlePath])
{
    NSLog(@"Path : %@",bundlePath);
    [pdfWebView loadData:[NSData dataWithContentsOfFile:bundlePath] MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];
    [self.view addSubview:pdfWebView];
}

答案 1 :(得分:0)

NSData *data = [NSData dataWithContentsOfFile: filename1];
[web loadData:data MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];

答案 2 :(得分:0)

这将是完美的。

UIWebView * webview = [[UIWebView alloc] initWithFrame:Frmaesize
NSString * path = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"pdf"];
NSURL * URL = [NSURL path];
NSURLRequest * request = [NSURLRequest requestWithURL:URL];
[webview loadRequest:request];
[self.view addsubView:webview];

答案 3 :(得分:0)

为您的代码尝试这种方式,

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:@"KeyToSelectedFile"]];
        NSString *filename1=[fileName stringByAppendingPathComponent:s1];

        web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
        web.delegate=self;
        web.backgroundColor=[UIColor whiteColor];
        NSData *data = [NSData dataWithContentsOfFile:filename1];
        [web loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:url];
        [self.view addSubview:web];