Iphone 在webview中显示PDF文件

Iphone 在webview中显示PDF文件,iphone,Iphone,我正在做一个应用程序,在webview中显示pdf文件,如下所示 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:@"KeyToSelectedFi

我正在做一个应用程序,在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而不崩溃。

我检查了这个,它工作正常,请尝试

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

这将完美地工作

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

对你的代码试试这种方法

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

dataWithContentOfFile需要nsstring值而不是url。文件在我的应用程序中不可用。文件在应用程序内存(目录)中可用。好的,然后我们必须从NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)获取路径;
        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];